模糊查詢的通用
存儲過程實現語句。
IF Exists(Select 1 From sysobjects Where Name='sp_search' And xType='P')
Drop Procedure sp_search
go
/*
模糊查詢的通用存儲過程
create by sxm,date 2009-7-14
參數:
@table_name 表名
@condition 條件
*/
create proc sp_search(@table_name varchar(200),@condition varchar(100))
with encryption
as
begin
declare @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_name
open cur_1
fetch cur_1 into @col_name
while @@fetch_status=0
begin
--組合查詢條件
set @str_cols=@str_cols + @col_name+' like ''%' + @condition+ '%''' + ' or '
fetch cur_1 into @col_name
end --while
close cur_1
deallocate cur_1
set @str_cols=left(@str_cols,len(@str_cols)-3)
--print @str_cols
set @strsql='select * from '+@table_name+' where '+ @str_cols
exec(@strsql)
end