CharIndex函数的应用-存数过程SQL

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
-------创建综合评估表[pr_create_quality_board 循环遍历-游标]
--create ALTER

create  PROCEDURE  [dbo].[pr_create_Evaluation22]
 @objectID nvarchar(1000) --/%生成的评估对象%/
as
--声明变量
 declare @pos int
 declare @oldPos int
 declare @tempstr varchar(100)

create table #temp_id
(
 id int
)

set @pos=1
set @oldPos=1
while @pos<len(@objectID)
begin
 set @pos=charindex(',',@objectID,@oldpos)
 if @pos>0
 begin
  set @tempstr=substring(@objectID,@oldpos,@pos-@oldpos)
  set @oldpos=@pos+1
 end
 else
 begin
  set @tempstr=substring(@objectID,@oldpos,len(@objectID)-@oldpos+1)
  set @pos=len(@objectID)
 end
 insert into #temp_id
 select @tempstr
end

select * from
#temp_id
go
declare @date datetime
set @date=getdate()
exec pr_create_Evaluation22 '1,2,3,4'


--print  Convert(char(10),GetDate(),120)   //这个是获取(年月日)的



这个例子用于 select * from fp_user where userid in (1,2,3,4)
1,2,3,4在存储过程作为传进的参数,用IN 语句,会当做整个字符串 无法解析,单个值,
利用分割字符串,创建临时表的功能查询
select * from fp_user where userid in (select  id from  #temp_id) 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/yingpp/archive/2009/09/28/1575410.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值