在存储过程里条件 in后面怎么放变量呀

OldEagle33032 某物联网公司 技术经理  2008-04-06 03:22:58
在存储过程里条件 in后面怎么放变量呀

declare @aa nvarchar(100)
set @aa= '1,2,3'
select ID from table1 where id in (@aa)
不知道这样怎么不行呀
...全文
209 点赞 收藏 15
写回复
15 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
UltraBejing 2008-05-01
不明LZ在说什么
回复
meiZiNick 2008-05-01
没遇到过这种情况.
回复
daemonys 2008-04-21
好,学习了
回复
hui_hui_2007 2008-04-07
这要用到动态sql

declare @s varchar(50)
set @s='''01'',''17'',''19'''

declare @str varchar(100)
set @str='select * from gzda where 部门 in (' + @s + ')'

exec (@str)
回复
wangxuelid 2008-04-07



if object_id('tb') is not null
drop table tb
create table tb (id varchar(10))
insert into tb values('aa')
insert into tb values('bb')
insert into tb values('cc')
insert into tb values('dd')
insert into tb values('ee')

declare @s as varchar(50)
declare @s1 as varchar(50)
declare @s2 as varchar(50)
set @s = 'aa'
set @s2 = 'bb'
set @s1 = 'cc'
select @s,@s2,@S1
Execute my_proce1 @s,@s1,@s2
go
if object_id('tb') is not null
drop procedure my_proce1

create procedure my_proce1
@str varchar(50),
@str1 varchar(50),
@str2 varchar(50)
as
begin
exec('select * from tb where id in ('''+ @str +''',''' + @str1 + ''',''' + @str2 + ''')')
end
--结果
aa
bb
cc
回复
wangxuelid 2008-04-07


if object_id('tb') is not null
drop table tb
create table tb (id varchar(10))
insert into tb values('1')
insert into tb values('2')
insert into tb values('3')
insert into tb values('4')
insert into tb values('5')
go
--查询原始数据
select * from tb
/*
id
----------
aa
bb
cc
dd
ee
(5 行受影响)
*/

go
if object_id('my_proce') is not null
drop proc my_proce

create procedure my_proce @str varchar(50)
as
begin
exec('select * from tb where id in (''' + @str + ''')')
end
go

declare @s as varchar(50)
set @s = '1'',''2'',''3'
--执行存储过程查询数据
EXECUTE my_proce @s
/*
id
----------
1
2
3
(3 行受影响)
*/

drop table tb
drop procedure my_proce

回复
huangjh_love 2008-04-07
[Quote=引用 1 楼 happyflystone 的回复:]
declare @aa nvarchar(100)
set @aa= '1,2,3'
exec('select ID from table1 where id in ('+ltrim(@aa)+')')
[/Quote]
回复
conan304 2008-04-07
declare @aa nvarchar(100)
set @aa='1,2,3'
exec('select [ID] from table1 where [id] in ('+@aa+')'


动态sql语句基本语法 
1 :普通SQL语句可以用Exec执行

eg: Select * from tableName
Exec('select * from tableName')
Exec sp_executesql N'select * from tableName' -- 请注意字符串前一定要加N

2:字段名,表名,数据库名之类作为变量时,必须用动态SQL

eg:
declare @fname varchar(20)
set @fname = 'FiledName'
Select @fname from tableName -- 错误,不会提示错误,但结果为固定值FiledName,并非所要。
Exec('select ' + @fname + ' from tableName') -- 请注意 加号前后的 单引号的边上加空格

当然将字符串改成变量的形式也可
declare @fname varchar(20)
set @fname = 'FiledName' --设置字段名

declare @s varchar(1000)
set @s = 'select ' + @fname + ' from tableName'
Exec(@s) -- 成功
exec sp_executesql @s -- 此句会报错



declare @s Nvarchar(1000) -- 注意此处改为nvarchar(1000)
set @s = 'select ' + @fname + ' from tableName'
Exec(@s) -- 成功
exec sp_executesql @s -- 此句正确

3. 输出参数
declare @num int,
@sqls nvarchar(4000)
set @sqls='select count(*) from tableName'
exec(@sqls)
--如何将exec执行结果放入变量中?

declare @num int,
@sqls nvarchar(4000)
set @sqls='select @a=count(*) from tableName '
exec sp_executesql @sqls,N'@a int output',@num output
select @num

回复
hery2002 2008-04-06
1楼
回复
补课 2008-04-06

declare sqlStr varchar (1000)
declare sqlWhere varchar(1000)
set sqlWhere='1,2,3'
set sqlStr='select ID FROM table1 where id in('+sqlWhere+')'
exec(sqlStr)
回复
piaoqifeng 2008-04-06
agree 1
回复
Limpire 2008-04-06
agree
回复
viva369 2008-04-06
动态SQL
回复
postfxj 2008-04-06
1樓正解
回复
-狙击手- 2008-04-06
declare @aa nvarchar(100)
set @aa= '1,2,3'
exec('select ID from table1 where id in ('+ltrim(@aa)+')')
回复
相关推荐
发帖
MS-SQL Server
创建于2007-09-28

3.3w+

社区成员

MS-SQL Server相关内容讨论专区
申请成为版主
帖子事件
创建了帖子
2008-04-06 03:22
社区公告
暂无公告