CREATE TABLE [TABLE1] (
[id] [int] IDENTITY (1, 1) NOT NULL ,
[name] [char] (10) COLLATE Chinese_PRC_CI_AS NULL ,
CONSTRAINT [PK_TABLE1] PRIMARY KEY CLUSTERED
(
[id]
) ON [PRIMARY]
) ON [PRIMARY]
GO
--1
declare @nvchSQL nvarchar(500)
declare @nvchNumbers nvarchar(50)
set @nvchNumbers = N'2,4,6'
set @nvchSQL = N'SELECT * FROM Table1 WHERE id IN (' + @nvchNumbers + N')'
print @nvchSQL
--exec sp_executesql @nvchSQL
exec(@nvchSQL)
--2
select * from table1 where id in (2,4,6)
declare @s nvarchar(50)
set @s = N'select * from table1 where id in '+ N'2,4,6'
exec(@s)
--3 第一种方法的另外表示方法 去除N也可以
declare @nvchSQL nvarchar(500)
declare @nvchNumbers nvarchar(50)
set @nvchNumbers = '2,4,6'
set @nvchSQL = 'SELECT * FROM Table1 WHERE id IN (' + @nvchNumbers + ')'
print @nvchSQL
--exec sp_executesql @nvchSQL
exec(@nvchSQL)