如果字段只是(1)(2)(3)(4)中的一个,比如(1)可以这样
use Testdb2
go
IF NOT OBJECT_ID('[t_Demo]') IS NULL
DROP TABLE [t_Demo]
GO
CREATE TABLE [t_Demo]
([ID] int identity(1,1) primary key not null,
[Cate] Nvarchar(20) null
)
go
INSERT [t_Demo]
SELECT '(1)' union all
SELECT '(2)' union all
SELECT '(3)' union all
SELECT '(4)' union all
SELECT '(2)' union all
SELECT '(1)'
go
Declare @vars nvarchar(100)
set @Vars='1,2,3,5'
Declare @strSql Nvarchar(max)
set @strSql='select * from [t_Demo] where Replace(Replace([Cate],''('',''''),'')'','''') in ('+@Vars+')'
print @strSql
exec (@strSql)
/*
select * from [t_Demo] where Replace(Replace([Cate],'(',''),')','') in (1,2,3,5)
(5 row(s) affected)
ID Cate
1 (1)
2 (2)
3 (3)
5 (2)
6 (1)
不用like,用=。自己拼字符串