存储过程传参

yubofighting 2011-10-20 01:38:52

怎么将下面的字符串当做存储过程的一个参数传到存储过程里去,并能执行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参数传进去,要怎么做?
...全文
113 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
Rotel-刘志东 2011-10-22
  • 打赏
  • 举报
回复
create proc test(@time)
as
begin
exec('select * from tb where id in('+@time+')')
end
go
exec test '''qwe'',''5656'',''545'',''344'',''233'',''123'''
pengxuan 2011-10-22
  • 打赏
  • 举报
回复

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'''
kensouterry 2011-10-21
  • 打赏
  • 举报
回复

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)

做一段小的字符串替换即可
lnmhfeng 2011-10-20
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 happyandsad 的回复:]

引用 5 楼 fredrickhu 的回复:
SQL code
表名或者字段名为变量的时候 需要用动态语句

create proc test(@time)
as
begin
exec('select * from tb where id in('+@time+')')
end

go
exec test '''qwe'',''5656'',''545'',''344'','……
[/Quote]
+++
小宏 2011-10-20
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 fredrickhu 的回复:]
SQL code
表名或者字段名为变量的时候 需要用动态语句

create proc test(@time)
as
begin
exec('select * from tb where id in('+@time+')')
end
go
exec test '''qwe'',''5656'',''545'',''344'',''233'',''123'''
[/Quote]
+++
中国风 2011-10-20
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 yubofighting 的回复:]

引用 5 楼 fredrickhu 的回复:
SQL code
表名或者字段名为变量的时候 需要用动态语句

create proc test(@time)
as
begin
exec('select * from tb where id in('+@time+')')
end
go

exec test '''qwe'',''5656'',''545'',''344'','……
[/Quote]

CTE与语句一样用

记录如果CTE前用语句加上;with a as (select 1 as ID) select * from a
-晴天 2011-10-20
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 yubofighting 的回复:]
引用 5 楼 fredrickhu 的回复:
SQL code
表名或者字段名为变量的时候 需要用动态语句

create proc test(@time)
as
begin
exec('select * from tb where id in('+@time+')')
end
go
exec test '''qwe'',''5656'',''545'',''344'',''2……
[/Quote]

可以建一个临时表,根据传入的参数进行动态查询,放到临时表中去,然后再在临时表的基础上做cte.
yubofighting 2011-10-20
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 fredrickhu 的回复:]
SQL code
表名或者字段名为变量的时候 需要用动态语句

create proc test(@time)
as
begin
exec('select * from tb where id in('+@time+')')
end
go
exec test '''qwe'',''5656'',''545'',''344'',''233'',''123'''
[/Quote]

cte中貌似不能用动态语句,看来要大改。。。
--小F-- 2011-10-20
  • 打赏
  • 举报
回复
表名或者字段名为变量的时候 需要用动态语句

create proc test(@time)
as
begin
exec('select * from tb where id in('+@time+')')
end
go
exec test '''qwe'',''5656'',''545'',''344'',''233'',''123'''
cjh200102 2011-10-20
  • 打赏
  • 举报
回复
楼上不错。
中国风 2011-10-20
  • 打赏
  • 举报
回复




create proc test(@time)
as
begin
select * from tb where ','+@time)+',' LIKE '%,'+RTRIM(ID)+',%'
end


GO
EXEC test 'qwe,5656,545,344,233,123'
中国风 2011-10-20
  • 打赏
  • 举报
回复

create proc test(@time)
as
begin
exec('select * from tb where id in('+@time+')')
end
go
exec test '''qwe'',''5656'',''545'',''344'',''233'',''123'''
中国风 2011-10-20
  • 打赏
  • 举报
回复
create proc test(@time)
as
begin
exec('select * from tb where id in('+@time+')')
end
go
傳參加上引號

34,588

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧