时间:2021-07-21人气:-
模糊查询的通用存储过程实现语句。IF Exists(Select 1 From sysobjects Where Name='sp_search' And xType='P')Drop Procedure sp_searchgo/*模糊查询的通用存储过程create by sxm,date 2009-7-14参数:@table_name 表名@condition 条件*/create proc sp_search(@table_name varchar(200),@condition varchar(100))with encryptionasbegindeclare @strsql varchar(8000)declare @col_name varchar(100)declare @str_cols varchar(8000)set @str_cols=''--查询表中的列名declare cur_1 cursor for select column_name from information_schema.columns where table_name=@table_nameopen cur_1fetch cur_1 into @col_namewhile @@fetch_status=0begin--组合查询条件set @str_cols=@str_cols + @col_name+' like ''%' + @condition+ '%''' + ' or 'fetch cur_1 into @col_nameend --whileclose cur_1deallocate cur_1set @str_cols=left(@str_cols,len(@str_cols)-3)--print @str_colsset @strsql='select * from '+@table_name+' where '+ @str_colsexec(@strsql)end
上篇:MYSQL数据库常用字符处理函数
下篇:提高MYSQL查询效率的三个有效的尝试