declare @ListId nvarchar(255)
SET @ListId='SELECT id FROM [table] WHERE id>10
SELECT *
FROM table where [id] not in (@ListId)我这样子写不行!
提示
在将 nvarchar 值 'SELECT id FROM [table] WHERE id>10' 转换成数据类型 int 时失败。
请问要怎么写,因为我要很多个地方用到 SELECT id FROM [table] WHERE id>10所以想提取出来
SET @ListId='SELECT id FROM [table] WHERE id>10
SELECT *
FROM table where [id] not in ('+@ListId+')'
SET @ListId='SELECT id FROM [table] WHERE id>10'EXEC('
SELECT *
FROM table where [id] not in ('+@ListId+')')
set @ListId = N''
SET @ListId= SELECT @ListId = @ListId + ',' + convert(nvarchar(10), id) FROM [table] WHERE fenshu>10declare @sql nvarchar(1000)set @sql = N'SELECT *
FROM table where [id] not in (' + @ListId + ')'
print @sql
SELECT *
FROM table where [id] not in ('+ltrim(@ListId)+')')
declare @ListId varchar(255)
SELECT @ListId=isnull(@ListId+',','')+id FROM [table] WHERE id>10
exec('SELECT * FROM table where id not in ('''+@ListId+''')')--或者直接
select * FROM [table] WHERE id<=10
--id是int型的话改下
declare @ListId varchar(255)
SELECT @ListId=isnull(@ListId+',','')+id FROM [table] WHERE id>10
exec('SELECT * FROM table where id not in ('+@ListId+')')
declare @ListId nvarchar(255)
SET @ListId='SELECT id FROM [table] WHERE id>10
--动态执行语句用exec
exec('SELECT * FROM table where [id] not in ('+@ListId+')')
神马动态sql最讨厌了...
你的id是不是有的不能隐式转换成数值型数据
declare @ListId nvarchar(255)
SET @ListId='SELECT id FROM [table] WHERE id>10'Exec ('
SELECT *
FROM table where [id] not in ('+@ListId+')
')