首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

where id in (.)解决办法

2012-04-21 
where id in (...)我想做一个查询,可以查询多条记录。类似于这样:SQL codeselect * from tblA where id in

where id in (...)
我想做一个查询,可以查询多条记录。
类似于这样:

SQL code
select * from tblA where id in ('00001','00003','00008')

我给用户了一个文本框,并给了用户在输入的时候,用逗号把内容分开。
然后,我发现问题不是我想的那么容易。
在我看来,sql语句会变成这样:
SQL code
select * from tblA where id in ('00001,00003,00008')


不知道我描述的是否清楚,如果需要,我会再补充。
劳烦各位帮帮忙。

[解决办法]
declare @v nvarchar(2000)
set @v = '00001,00003,00008';
set @v = ''' + replace(@v,',',''',''') + ''';
select * from tblA where id in (@v);
[解决办法]
你的意思就是动态拆分呗。
[解决办法]
SQL code
  declare @s1 varchar(1000)  set @s1=right(replace(','+@s,',',''' as S union select '''),len(replace(','+@s,',',''' as S union select '''))-12)+''''  exec(@s1)
[解决办法]
替换, 变成','
[解决办法]
探讨

替换, 变成','

[解决办法]
SQL code
--用临时表作为数组create   function   f_split(@c   varchar(2000),@split   varchar(2))   returns   @t   table(col   varchar(20))   as       begin             while(charindex(@split,@c)<>0)           begin             insert   @t(col)   values   (substring(@c,1,charindex(@split,@c)-1))             set   @c   =   stuff(@c,1,charindex(@split,@c),'')           end         insert   @t(col)   values   (@c)         return       end   go       select   *   from   dbo.f_split('dfkd,dfdkdf,dfdkf,dffjk',',')       drop   function   f_split col                                       --------------------     dfkd   dfdkdf   dfdkf   dffjk       (所影响的行数为   4   行)
[解决办法]
这样不行吗 将,替换成',' 转义失败?
SQL code
where id in (replace(@id,',',''',''')
[解决办法]
探讨

这样不行吗 将,替换成',' 转义失败?
SQL code
where id in (replace(@id,',',''',''')

[解决办法]
select * from tblA where id in ('00001','00003','00008')

-->

select * from tblA where charindex(id ,'00001,00003,00008') > 0

select * from tblA where charindex(id ,你的字符串变量) > 0

[解决办法]
SQL code
SELECT ''''+REPLACE('1,2,3,5,6',',',''',''')+''''--结果为 '1','2','3','5','6'
[解决办法]
SQL code
--select * from tblA where id in ('00001,00003,00008')select * from tblA where charindex(','+ltrim(id)+',',','+'00001,00003,00008'+',')>0 

热点排行