34,590
社区成员
发帖
与我相关
我的任务
分享
'+@strTest+'
--定义变量
declare @strTest varchar(4000)
declare @tempsql varchar(4000)
--赋值
set @strTest = '1,2,3,5,7'
set @tempsql=' select * from tableOne where A in ('+strTest')'
--执行
exec(@tempsql)
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'tableOne')
AND type in (N'U'))
DROP TABLE tableOne
GO
---->建表
create table tableOne([A] bigInt)
insert tableOne
select '1' union all
select '2' union all
select '8'
GO
declare @strTest varchar(4000)
set @strTest = '1,2,3,5,7'
SELECT *
FROM tableOne
where CHARINDEX(','+ltrim([A]),','+@strTest+',')>0
declare @strTest varchar(4000)
set @strTest = '1,2,3,5,7'
declare @sql varchar(4000)
set @sql=' select * from tableOne where A in ('+strTest')'
exec(@sql)
--> 数据库版本:
--> Microsoft SQL Server 2008 (RTM) - 10.0.1600.22
--> 测试数据:tableOne
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'tableOne')
AND type in (N'U'))
DROP TABLE tableOne
GO
---->建表
create table tableOne([A] varchar(9))
insert tableOne
select '1,2,3,5,7' union all
select '2,6'
GO
declare @strTest varchar(4000)
set @strTest = '1,2,3,5,7'
SELECT *
FROM tableOne
where CHARINDEX(','+[A],','+@strTest+',')>0
--> 删除表格
--DROP TABLE tableOne