sql语句中 where 字段 in ('1','2','3') 这个in括号里的值有效 但我先定义一个字符串@str=

sql语句中 where 字段 in ('1','2','3') 这个in括号里的值有效 但我先定义一个字符串@str='''1'',''2'',''3''' where 字段 in (@str)这样失败了,求帮助
花妖精花妖精 1年前 已收到1个回答 举报

tlbb6302 花朵

共回答了17个问题采纳率:100% 举报

create table #temp(code varchar(20))
x09x09x09x09x09x09insert into #temp select '1'
x09x09x09x09x09x09insert into #temp select '4'
x09x09x09x09x09x09insert into #temp select '3'
x09x09x09x09x09x09insert into #temp select '2'
x09x09x09x09x09x09
x09x09x09x09x09x09declare @str varchar(20)='''1'',''2'',''3'''
x09x09x09x09x09x09declare @lsql varchar(100)x09x09x09x09x09x09
x09x09x09x09x09x09set @lsql = 'select * from #temp where code in ('+@str+')'
x09x09x09x09x09x09exec (@lsql)

1年前 追问

6

举报 tlbb6302

亲,可用别忘采纳

花妖精花妖精 举报

消息 139,级别 15,状态 1,第 0 行
不能向局部变量赋予默认值。
消息 137,级别 15,状态 2,第 3 行
必须声明标量变量 "@str"。

举报 tlbb6302

是SQL server不?你全部选中,然后执行,我亲测的·不可能有错的

花妖精花妖精 举报

是的,但你给的思路很好,我建了函数用,可以了,刚学就是难啊,今天做好了,感觉好爽

可能相似的问题
Copyright © 2024 YULUCN.COM - 雨露学习互助 - 17 q. 2.314 s. - webmaster@yulucn.com