in括号中逗号的问题,比较简单

xielk 2005-09-14 03:11:52
select * from sx where cast (id as char) in ('1','2') 返回结果

select * from sx where cast (id as char) in ( ' ''1'',''2'' ' )没有结果返回

select ' ''1'',''2''' ='1','2'

问怎样才能使
Set @s='1','2'
select * from sx where cast (id as char) in (@s)有结果返回
也就是说@s返回的值应该为'1','2'
...全文
212 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
zlp321002 2005-09-14
  • 打赏
  • 举报
回复
--测试环境
declare @t table( a varchar(20))
insert into @t select '''1'''
insert into @t select '''2'''
insert into @t select '''4'''
insert into @t select '''3'''
--查询语句
select * from @t where charindex(','+a+',',',''1'',''2'',')>0
--结果
a
--------------------
'1'
'2'

zlp321002 2005-09-14
  • 打赏
  • 举报
回复
--try
select * from 表 where charindex(','1','2',',','+字段+',')>0
vivianfdlpw 2005-09-14
  • 打赏
  • 举报
回复
declare @s varchar(10)
Set @s='''1'',''2'''
declare @sql nvarchar(100)
set @sql='declare p_cursor cursor for select * from sx where charindex(cast (id as char),@s)>0'
exec sp_executesql @sql,N'@s varchar(10)',@s

open p_cursor
xielk 2005-09-14
  • 打赏
  • 举报
回复
想问一个
Set @s='''1'',''2'''
exec('select * from sx where cast (id as char) in ('+@s+')')
如果是游标的引用应该怎么写
declare p_cursor cursor for exec 'select * from sx where cast (id as char) in ('+@s+')'
???
wangdehao 2005-09-14
  • 打赏
  • 举报
回复
一句话,在sqlserver中,2个'表示一个'
MorningTea 2005-09-14
  • 打赏
  • 举报
回复
in
一定要这样形式:
in ('1','2')
所以你最终的sql必须凑成如上的形式
用动态sql,遇到单引号,2个代表一个

wgsasd311 2005-09-14
  • 打赏
  • 举报
回复
select * from sx where cast (id as char) in ( ' ''1'',''2'' ' )没有结果返回
----------------IN后面会被认为是传递一个参数,此参数值为:‘1’,‘2’
如要达到你想要的结果即两个参数,需动态SQL语句:
declare @sql varchar(8000)
set @sql= 'select * from sx where cast (id as char) in ('+ ' ''1'',''2'' '+')'
exec(@sql)
pearl321 2005-09-14
  • 打赏
  • 举报
回复
select * from sx where cast (id as char) in (char(39)+'1'+char(39)+char(44)+char(39)+'2'+char(39))
lizhaogui 2005-09-14
  • 打赏
  • 举报
回复
select * from sx where cast (id as char) in ( ''1'',''2'' )
vivianfdlpw 2005-09-14
  • 打赏
  • 举报
回复
--创建测试环境
create table sx
(
id char(1)
)
insert sx
select '1' union select '2'

--测试
declare @s varchar(10)
Set @s='''1'',''2'''
exec('select * from sx where cast (id as char) in ('+@s+')')

--删除测试环境
drop table sx

--结果
/*
id
----
1
2
*/
vivianfdlpw 2005-09-14
  • 打赏
  • 举报
回复
Set @s='''1'',''2'''
exec('select * from sx where cast (id as char) in ('+@s+')')

34,594

社区成员

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

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