34,588
社区成员
发帖
与我相关
我的任务
分享
怎么将下面的字符串当做存储过程的一个参数传到存储过程里去,并能执行in
'qwe','5656','545','344','233','123'
如
create proc test(@time)
as
begin
select * from tb where id in(@time)
end
exec test 'qwe','5656','545','344','233','123'--将字符串当做@time参数传进去,要怎么做?
create proc test(@time)
as
begin
exec('select * from tb where id in('+@time+')')
end
go
exec test '''qwe'',''5656'',''545'',''344'',''233'',''123'''
if object_id('test','P') is not null
drop procedure test
go
create proc test(@time varchar(100))
as
exec('select * from tb where id in('+@time+')')
go
exec test '''101'',''102'''
declare @execSQL NVARCHAR(4000)
declare @param NVARCHAR(1000)
declare @origexecSQL nvarchar(4000)
set @origexecSQL=N'select * from t1 where flowno in (%idset%)'
set @param=N'@idset varchar(20) '
set @execSQL=REPLACE(@origexecSQL, '%idset%', '1,2,3')
print @execSQL
set @execSQL=REPLACE(@origexecSQL, '%idset%', ' ''1'',''2'',''3'' ')
print @execSQL
exec(@execSQL)
表名或者字段名为变量的时候 需要用动态语句
create proc test(@time)
as
begin
exec('select * from tb where id in('+@time+')')
end
go
exec test '''qwe'',''5656'',''545'',''344'',''233'',''123'''
或
create proc test(@time)
as
begin
select * from tb where ','+@time)+',' LIKE '%,'+RTRIM(ID)+',%'
end
GO
EXEC test 'qwe,5656,545,344,233,123'
create proc test(@time)
as
begin
exec('select * from tb where id in('+@time+')')
end
go
exec test '''qwe'',''5656'',''545'',''344'',''233'',''123'''