注册 登录
编程论坛 SQL Server论坛

请教:SQL语句

Deyee 发布于 2007-07-13 16:33, 1031 次点击

-- 查询语句,执行没问题。
declare @c varchar(100)
set @c = '1,2,3'
exec('select * from DyStfDir where StfJD in (' + @c + ')')

-- 判断语句,执行也没问题
if exists(select * from DyStfDir where StfJD in (1,2,3))
print 1111
else
print 2222

-- 想将@c放入exists()中就不行了。
declare @c varchar(100)
set @c = '1,2,3'
if exists('select * from DyStfDir where StfJD in (' + @c + ')')
print 1111
else
print 2222

请帮忙,多谢。

10 回复
#2
bygg2007-07-13 17:49
declare @c varchar(100)
set @c = '1,2,3'
if exists(select * from DyStfDir where StfJD in (@c))
print 1111
else
print 2222
#3
Deyee2007-07-14 10:09
不行呀,我试过了,提示:

服务器: 消息 245,级别 16,状态 1,行 3
将 varchar 值 '1,2,3' 转换为数据类型为 int 的列时发生语法错误。
#4
sam20802007-07-14 11:32
declare @c varchar(100)
set @c = '1,2,3'
if exists( select * from kyyytable where ky_server in (' + ''''+@c+'''' + ') )
print 1111
else
print 2222
#5
sam20802007-07-14 11:34
不过不理解楼主这样做的目的
#6
Deyee2007-07-16 09:26
多谢各位,这种方法是行不通的,不过可以使用CHARINDEX()函数。
#7
sam20802007-07-16 13:34
暴汗一个。。。
原来都被耍。。。
#8
Deyee2007-07-17 11:04
探讨问题而已,谁被耍了???
我是没办法才用CHARINDEX()函数的
#9
nongen2007-07-18 15:21
服务器: 消息 245,级别 16,状态 1,行 3
将 varchar 值 '1,2,3' 转换为数据类型为 int 的列时发生语法错误。
1,2,3不是记录,不是数组,汗一个,只是一个串,判断一个INT值在不在里面啊。再汗一个~~~~~~~~~~~
#10
Deyee2007-07-18 15:45
问题就在这里,所以我反转过来,将StfJD转换为字符串,再用CHARINDEX()比较,高手吗?
#11
Deyee2007-07-18 15:47

declare @c varchar(100)
set @c = '1,2,3'
if exists(select * from DyDir where charindex(cast(DyJD as varchar),@c)>0)
print 1111
else
print 2222

1