语句如下:
DECLARE @companyid VARCHAR (100)
SET @companyid='102,103,106,107,108'
SELECT * FROM oCompany WHERE CompanyID IN (@companyid)
得不到结果。
如果where CompanyID IN(102,103,106,107,108) 是可以得到结果的。
------解决方案--------------------
用动态SQL,
DECLARE @companyid VARCHAR(100),@tsql varchar(6000)
SET @companyid='102,103,106,107,108'
select @tsql='SELECT * FROM oCompany WHERE CompanyID IN ('+@companyid+')'
exec(@tsql)
------解决方案--------------------
楼主也可以改一下:
DECLARE @companyid VARCHAR (100)
SET @companyid='102,103,106,107,108'
SELECT *
FROM oCompany
WHERE charindex(CompanyID, @companyid ) > 0