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

小疑点,一时搞不定,请高手解答,

2012-01-19 
小问题,一时搞不定,请高手解答,在线等!!declare@useridsvarchar(2000)set@userids 5,6,7,8select*fromCom

小问题,一时搞不定,请高手解答,在线等!!
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)

热点排行