我暈,你怎麼用雙引號啊 create table tb (a nvarchar(50),b nvarchar(30)) go insert tb select '2,4,6,8','admin' insert tb select '4,6,7,9','vuser' insert tb select '2,3,4,6','tuser' go declare @d int SET @d=6 select * from tb where charindex('6',a)>0go drop table tb go
那樣查的話,如果其中有16也會被查出來的,改成這樣更好一些 select * from tb where charindex(','+cast(@d as varchar(10))+',',','+a+',')>0
go
insert tb select "2,4,6,8","admin"
insert tb select "4,6,7,9","vuser"
insert tb select "2,3,4,6","tuser"
go
declare @d int
SET @d=6
select * from tb where charindex('6',a)>0go
drop table tb
go服务器: 消息 207,级别 16,状态 3,行 1
列名 '2,4,6,8' 无效。
服务器: 消息 207,级别 16,状态 1,行 1
列名 'admin' 无效。
服务器: 消息 207,级别 16,状态 1,行 2
列名 '4,6,7,9' 无效。
服务器: 消息 207,级别 16,状态 1,行 2
列名 'vuser' 无效。
服务器: 消息 207,级别 16,状态 1,行 3
列名 '2,3,4,6' 无效。
服务器: 消息 207,级别 16,状态 1,行 3
列名 'tuser' 无效。(所影响的行数为 0 行)
==========================================
谢谢你教我用这个函数,不过还是有别的问题。
create table tb (a nvarchar(50),b nvarchar(30))
go
insert tb select '2,4,6,8','admin'
insert tb select '4,6,7,9','vuser'
insert tb select '2,3,4,6','tuser'
go
declare @d int
SET @d=6
select * from tb where charindex('6',a)>0go
drop table tb
go
select * from tb where charindex(','+cast(@d as varchar(10))+',',','+a+',')>0