日期:2014-05-18  浏览次数:20412 次

一个Sql查询集合的问题!
在开发一个程序的时候,遇到一个Sql问题,数据库 SqlServer 2000

如下这个SQL语句是可以执行成功的!
select * from OS_TreeAddress where OS_Description!=1 and ID in (2,3,4,5,6,7)

但问题是我的2,3,4,5,6,7,这个,现在是要从另外一个表查出来的!

select * from OS_TreeAddress where OS_Description!=1 and ID in 
(select OS_PurviewID from OS_Purview where Id in (select OS_DeptID from OS_UserInfo where ID=1))
这个就查不出来了!报如下错误:

服务器: 消息 245,级别 16,状态 1,行 1
将 varchar 值 '2,3,4,5,6,7' 转换为数据类型为 int 的列时发生语法错误。


如下的语句查出来的结果2,3,4,5,6,7 ,
select OS_PurviewID from OS_Purview where Id in (select OS_DeptID from OS_UserInfo where ID=1)


我现在要如何写Sql语句,才能和select * from OS_TreeAddress where OS_Description!=1 and ID in (2,3,4,5,6,7)
这个语句的效果一样呢?

------解决方案--------------------
将OS_PurviewID 转换为int
将OS_DeptID 转换为int
具体的情况要你自己才知道了.

如:
select * from OS_TreeAddress where OS_Description!=1 and ID in
(select cast(OS_PurviewID as int) from OS_Purview where Id in (select cast(OS_DeptID as int) from OS_UserInfo where ID=1))