我有以下查询
DEClare @Plan_ID nvarchar(100)
set @Plan_ID=REPLACE('2151,1886',',',''',''')
和
SELECT distinct Plan_Dict_Id from REF_Plan_Dictionary WHERE
CAST(Plan_Dict_Id as int) in (@Plan_ID),
Pls help , Plan_Dict_Id
数据类型是 INT
, 我想传递的值到哪里 , 但得到错误 "转换失败时转换的 varchar
值'2151','1886'改为数据类型为int。"
如果这是SQL Server的后期版本,那么你可以做这样的事情......。
DECLARE @Plan_ID nvarchar(100)
SET @Plan_ID= '2151,1886'
SELECT DISTINCT Plan_Dict_Id
FROM REF_Plan_Dictionary d
JOIN string_split(@Plan_ID, ',') s ON d.Plan_Dict_Id = s.[value]
如果你使用的是旧版本,你可以把整个语句放在一个字符串中,然后执行它,但是你的第一行有一个问题,因为你没有在字符串周围添加引号。我已经在下面交代了。
DEClare @Plan_ID nvarchar(100) set @Plan_ID=REPLACE('2151,1886',',',''',''')
DECLARE @sql nvarchar(max)
SET @SQL = 'select distinct Plan_Dict_Id from REF_Plan_Dictionary where CAST(Plan_Dict_Id as int) in (''' + @Plan_ID + ''')'
EXEC (@sql)