日期:2014-05-17 浏览次数:20736 次
----------------------------------------------------------------
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2013-12-05 16:27:42
-- Version:
-- Microsoft SQL Server 2012 (SP1) - 11.0.3128.0 (X64)
-- Dec 28 2012 20:23:12
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: )
--
----------------------------------------------------------------
--> 测试数据:[huang]
if object_id('[huang]') is not null drop table [huang]
go
create table [huang]([id] int,[name] varchar(6))
insert [huang]
select 1,'数据线' union all
select 2,'充电器' union all
select 3,'说明书' union all
select 4,'电池' union all
select 5,'耳机'
--------------开始查询--------------------------
DECLARE @a VARCHAR(64)
SET @a='说明书,电池,数据线'
select * from [huang]
WHERE CHARINDEX(name,@a,1)>0
----------------结果----------------------------
/*
id name
----------- ------
1 数据线
3 说明书
4 电池
*/
create table #tab(id int,name varchar(50))
insert into #tab
select 1,'数据线' union all
select 2,'充电器' union all
select 3,'说明书' union all
select 4,'电池' union all
select 5,'耳机'
declare @a varchar(50),@sql varchar(max),@b varchar(50)
set @a='说明书,电池,数据线'
select @b=replace(@a,',',''',''')
set @sql='
select * from #tab
where name in ('''+@b+''')'
exec (@sql)
id name
----------- --------------------------------------------------
1 数据线
3 说明书
4