General Stored Procedure of fuzzy query

Source: Internet
Author: User

Copy codeThe Code is as follows:
IF Exists (Select 1 From sysobjects Where Name = 'SP _ search' And xType = 'P ')
Drop Procedure sp_search
Go
/*
General Stored Procedure of fuzzy query
Create by sxm, date 2009-7-14
Parameters:
@ Table_name 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 =''
-- Query the column name in the table
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
-- Combined Query Conditions
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


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.