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

请问

2012-02-20 
请教表aidcontent111,22,33,44,55,66277,88,99----------代码declare@contentvarchar(200)selecttop1@cont

请教
表a
id                     content
1                       11,22,33,44,55,66
2                       77,88,99

----------代码
declare   @content   varchar(200)
select   top   1   @content=content   from   a
select   *   from   orders   where   content   in(@content)

----------如何实现以下查询的结果
select   *   from   others   where   id   in(11,22,33,44,55,66)

[解决办法]
declare @content varchar(200)
select top 1 @content=content from a
exec( 'select * from orders where content in( '+@content+ ') ')
[解决办法]
declare @content varchar(200)
select top 1 @content=content from a
exec( 'select * from orders where id in( '+@content+ ') ')

[解决办法]
create table A
(
id int,
content varchar(100)
)

insert A select 1, '11,22,33,44,55,66 '
insert A select 2, '77,88,99 '

create table B
(
T int
)
insert B select 11
insert B select 22
insert B select 88

select * from B where charindex(cast(T as varchar),(select top 1 content from A ))> 0
[解决办法]
--改用CharIndex,可以不用使用動態SQL語句
Select T1.* from orders T1
Inner Join a T2
On CharIndex( ', ' + Cast(T1.id As Varchar) + ', ', ', ' + T2.content + ', ') > 0
Where T2.id = (Select TOP 1 id From a)

热点排行