日期:2014-05-19  浏览次数:20460 次

小问题,一时搞不定,请高手解答,在线等!!
declare   @userids   varchar(2000)
set   @userids= '5,6,7,8 '
select   *   from   CompanyJob_List   where   ID   in   (@userids)   order   by   id

上面的ID为int型的自增自段,请都如何查询出 '5,6,7,8 '这四条记录?

------解决方案--------------------
declare @userids varchar(2000),@sql varchar(8000)
set @userids= '5,6,7,8 '
set @sql= 'select * from CompanyJob_List where ID in ( '+@userids+ ') order by id '
exec(@sql)

------解决方案--------------------
declare @userids varchar(2000)
set @userids= '5,6,7,8 '
exec( 'select * from CompanyJob_List where ID in ( '+@userids+ ') order by id ')
------解决方案--------------------
--方法一
declare @userids varchar(2000)
set @userids= '5,6,7,8 '
select * from CompanyJob_List where CharIndex( ', ' + Rtrim(ID) + ', ', ', ' + @userids + ', ') > 0 order by id
------解决方案--------------------
declare @userids varchar(2000),@sql varchar(8000)
set @userids= '5,6,7,8 '
set @sql= 'select * from CompanyJob_List where ID in ( '+@userids+ ') order by id '
create table CompanyJob_List(id int identity(1,1),name varchar(8))
insert into CompanyJob_List(name)
select 'a '
union select 'b '
union select 'c '
union select 'd '
union select 'e '
union select 'f '
union select 'g '
union select 'h '
union select 'i '
union select 'j '
select * from CompanyJob_List
exec(@sql)