SQL简单语句

智者潜行 2009-03-26 03:22:32

select * from Operator where Username in('admin2','cai','admin','ccccccc')

-------------------------------------
declare @a varchar(500)
set select @a=[U_power] from UserGroup where U_ID='1'


begin
select * from Operator where Username in(''''+(replace(@a,',',''','''))+'''')
end
-------------------------

@a的值是admin2,cai,admin,ccccccc'
为什么执行第一句就有数据
执行下行的就没数据
...全文
67 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
sdhdy 2009-03-26
  • 打赏
  • 举报
回复
declare @a varchar(500)
--或者
set @a=(select [U_power] from UserGroup where U_ID='1')


begin
exec ('select * from Operator where Username in('+@a+')')
end
智者潜行 2009-03-26
  • 打赏
  • 举报
回复
对,只有你的可以,其它答案都不可以.

就是把查询得到的数据admin2,cai,admin,ccccccc
替换成'admin2','cai','admin','ccccccc'
再进行查找
[Quote=引用 7 楼 dawugui 的回复:]
引用 5 楼 lfoy112 的回复:
我想问是怎么回事..麻烦达人回答下引用 1 楼 dawugui 的回复:
SQL codeselect * from Operator where charindex(Username ,@a) > 0

你那个写法不对.得用楼上的动态SQL才行.
[/Quote]
中国风 2009-03-26
  • 打赏
  • 举报
回复
declare @a varchar(500)
set @a=','
select @a=@a+[U_power]+',' from UserGroup where U_ID='1'


begin
select * from Operator where patindex(','+Username+',',@a)>0
end


declare @a varchar(500)
set @a=','
select @a=@a+[U_power]+',' from UserGroup where U_ID='1'


begin
select * from Operator where @a like '%,'+Username+',%'
end

dawugui 2009-03-26
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 lfoy112 的回复:]
我想问是怎么回事..麻烦达人回答下引用 1 楼 dawugui 的回复:
SQL codeselect * from Operator where charindex(Username ,@a) > 0
[/Quote]
你那个写法不对.得用楼上的动态SQL才行.
-狙击手- 2009-03-26
  • 打赏
  • 举报
回复

估计你的意思应该是这样


declare @a varchar(500)
select @s = isnull(@a+''',''','''')+[U_power] from UserGroup where U_ID='1'
set @s = @s +''''

exec('select * from Operator where Username in('+@a+')')
智者潜行 2009-03-26
  • 打赏
  • 举报
回复
我想问是怎么回事..麻烦达人回答下[Quote=引用 1 楼 dawugui 的回复:]
SQL codeselect * from Operator where charindex(Username ,@a) > 0
[/Quote]
sdhdy 2009-03-26
  • 打赏
  • 举报
回复
--或者
set @a=(select [U_power] from UserGroup where U_ID='1')
sdhdy 2009-03-26
  • 打赏
  • 举报
回复
select @a=[U_power] from UserGroup  where U_ID='1'
-狙击手- 2009-03-26
  • 打赏
  • 举报
回复
declare @a varchar(500)
set select @a=[U_power] from UserGroup where U_ID='1'


---
declare @a varchar(500)
select @a=[U_power] from UserGroup where U_ID='1'
dawugui 2009-03-26
  • 打赏
  • 举报
回复
select * from Operator where charindex(Username ,@a) > 0

34,593

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧