但是如果我直接写select name from table1 where convert(varchar(50),id) in ('1','2') 就可以出来结果
to:whzww(无家的猫) 将 varchar 值 ''1','2'' 转换为数据类型为 int 的列时发生语法错误。????我当然有测试的啊!
skyboy0720(飞)select name from table1 where convert(varchar(50),id) in (select Pid from table2 where id=1) 这句 如果改成 select name from table1 where id in (select Pid from table2 where id=1) 运行,就会报错 :将 varchar 值 ''1','2'' 转换为数据类型为 int 的列时发生语法错误。
如果你那边不出错,那就是我的sqlserver坏了?
select name from table1 where charindex(convert(varchar(50),id),(select Pid from table2 where id=1))>0
哦,你说select name from table1 where id in (select Pid from table2 where id=1) 这句啊 当然会错啊,你还不明白啊??试试就知道了!declare @a varchar(20) set @a='''1'',''2''' select name from table1 where convert(varchar(50),id) in (@a)问题的所在如果in()里面的内容是动态的取得,就相当于你是用 '1'(这里或者是1)=('1','2')整个表达式,当然没有结果,不知道你明白没!
select name from table1 where convert(varchar(50),id) in (select Pid from table2 where id=1) 这句话有问题,你现在选的是Pid而Convert(varchar(50),id)却是id,这是两个不同的字段,当然没有你要值!
真的爱你 的方法就可以啊select name from tabl1 where charindex(convert(varchar(50),id),(select Pid from tabl2 where id=1))>0
他的方法要改一下select name from tabl1 where charindex(''''+convert(varchar(50),id)+'''',(select Pid from tabl2 where id=1))>0 要不就会多出首尾两个不匹配的 谢谢各位了终于搞定 可以回家吃饭了 饿死俺也
--------这是sql给的前后两个‘ 是隐含的 你用查询分析器查询,返回结果的时候看不到但是如果你引发系统错误 就会报给你
将 varchar 值 ''1','2'' 转换为数据类型为 int 的列时发生语法错误。????我当然有测试的啊!
这句 如果改成
select name from table1 where id in (select Pid from table2 where id=1)
运行,就会报错 :将 varchar 值 ''1','2'' 转换为数据类型为 int 的列时发生语法错误。
这句啊
当然会错啊,你还不明白啊??试试就知道了!declare @a varchar(20)
set @a='''1'',''2'''
select name from table1 where convert(varchar(50),id) in (@a)问题的所在如果in()里面的内容是动态的取得,就相当于你是用
'1'(这里或者是1)=('1','2')整个表达式,当然没有结果,不知道你明白没!
这句话有问题,你现在选的是Pid而Convert(varchar(50),id)却是id,这是两个不同的字段,当然没有你要值!
这样的返回结果会多两个 如果 pid 里面记录的是 '11','17'那么返回的结果是
1
7
11
17
四条数据
我知道问题出在这里
select name from table1 where id in (select Pid from table2 where id=1)现在是有什么办法能解决这个
现在要做的是,我在表二中选择一条记录,然后根据记录的 [PID] 值 查出所有的表一相关数据PS: 数据库是老板设计的,没办法更改,要不就不用这么麻烦了
郁闷阿
要不就会多出首尾两个不匹配的
谢谢各位了终于搞定 可以回家吃饭了 饿死俺也