存储过程中自动拆分输入框里的字符,然后跟表里的两三个字段去匹配
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 行)
相关文章推荐
- mssql 和 mysql的区别,让自动增长列自动递增就必须指定其他列名,mysql创建事务,如果插入的字符多余字段的值,会报错?截取插入?带输出参数的存储过程,带If else判断
- MyBatis拦截器动态修改SQL语句及参数值(场景:查询中字段值中特殊字符自动转义)
- mysql insert 空字符报错 double类型字段插入空字符串时自动转为0
- 对字段值按字符拆分多行方法
- 基于javascript实现仿百度输入框自动匹配功能
- 基于javascript实现仿百度输入框自动匹配功能
- 实用文本框输入字符后下拉菜单内容自动匹配效果
- JS实现自动匹配搜索字符
- JS实现仿百度输入框自动匹配功能的示例代码
- JS实现仿百度输入框自动匹配功能的示例代码
- sql server中将一个字段根据某个字符拆分成多个字段显示
- 输入框输入文字字符匹配
- SQL 中查询某个字段数据匹配字符不存在
- input输入框输满3个字符自动跳到下一个input输入框
- 查询表里的字段,然后以字符串的格式输出,再用分隔符分开,成对出现
- 利用js 进行输入框自动匹配字符的小例子
- 往数据表中插入 "字段名 字符串" , 对于非字符字段,采用 "递增整形数字" 代替(日期字段会自动转变为对应日期)
- input required字段;django input输入框不填写会自动变红如何修改
- NSComboBox输入字符时自动打开下拉菜单并匹配
- Android 之 自动匹配字符AutoCompleteTextView