您的位置:首页 > 其它

存储过程中自动拆分输入框里的字符,然后跟表里的两三个字段去匹配

2006-06-23 16:19 309 查看




































我爱北京天安门



天安门上太阳升



伟大领袖毛主席



指引我们向前进






































--创建测试表---------------------------------------

create table table1([name] varchar(100),type int)

--插入测试数据--------------------------------------

insert table1 select '商品名',1

insert table1 select '商品型号',1

insert table1 select '品牌',1

insert table1 select '商品名',2

insert table1 select '商品型号',2

insert table1 select '品牌',2

insert table1 select '商品名商品型号品牌',1

--创建存储过程-----------------------------------

alter procedure dbo.p_SplitKeys

@type int,@searchkey varchar(1000),@split varchar(10)

as

declare @v_split_tb table([key] varchar(100))

declare @v_where varchar(1000)

set @v_where=''

while(charindex(@split,@searchkey)>0)

begin

insert @v_split_tb select left(@searchkey,charindex(@split,@searchkey)-1)

set @searchkey=stuff(@searchkey,1,charindex(@split,@searchkey),'')

end

insert @v_split_tb select @searchkey

select @v_where=@v_where+'[name] like '+'''%'+[key]+'%'''+' and ' from @v_split_tb

set @v_where=stuff(@v_where,len(@v_where)-3,5,'')

exec('select * from table1 where type='+@type+' and
'+@v_where)

Go

--执行存储过程---------------------------------

exec dbo.p_SplitKeys 1,'商品名,商品型号,品牌',','

--结果------------------------------------------

name type

---------------------------------------------------------------------------------------------------- -----------

商品名商品型号品牌 1

(所影响的行数为 1 行)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐