?
Solved

sql server search query

Posted on 2009-12-23
4
Medium Priority
?
143 Views
Last Modified: 2012-05-08
place search term in 'SEARCH_TERM_WE_ARE_LOOKING_FOR'

this query will find a search term in a database of type varchar or int

but it does not find
[orderid] [int] IDENTITY(1,1) NOT NULL,
declare @sql varchar(8000), @tbl varchar(255), @col varchar(255), @data varchar(50)
set @data = 'SEARCH_TERM_WE_ARE_LOOKING_FOR'
declare cur_tbl cursor for
      select a.name, b.name from sysobjects a, syscolumns b, systypes c where a.id = b.id and a.type = 'U' and c.xtype = b.xtype and c.name in ( 'varchar', 

'nvarchar', 'text', 'ntext' )
open cur_tbl
fetch next from cur_tbl into @tbl, @col
while @@fetch_status = 0
begin
      set @sql = '
                        if exists (select * from [' + @tbl + '] where convert( varchar(255), [' + @col + '] ) like ''%' + @data + '%'')
                              select tbl=''' + @tbl + ''', col=''' + @col + ''', [' + @col + '], * from [' + @tbl + '] where convert( varchar(255), [' + @col + '] ) like ''%' + @data 

+ '%''
                        '
      exec(@sql)
 
      fetch next from cur_tbl into @tbl, @col
end
close cur_tbl
deallocate cur_tbl

Open in new window

0
Comment
Question by:rgb192
4 Comments
 
LVL 59

Expert Comment

by:Bill Prew
ID: 26116483
If the column you are trying to search actually contains the string you show:

[orderid] [int] IDENTITY(1,1) NOT NULL,

then the brackets will confuse the LIKE clause.  You can work around this by specifying an ESCAPE character, take a look at the following:

http://msdn.microsoft.com/en-us/library/ms179859.aspx

~bp
0
 
LVL 10

Accepted Solution

by:
lof earned 2000 total points
ID: 26116494
it is not looking for integers at the moment.
just add in the sixth line after 'ntext' and before )

, 'int'

so it should be

'nvarchar', 'text', 'ntext', 'int' )

and it will work
0
 
LVL 25

Expert Comment

by:jogos
ID: 26116520
and you are sure this text is exactly written like the string you concatinate?

Take the proof  before the  
         exec(@sql)
add a statement to let you show your sql
        print @sql

Now you see what is executed, copy that and paste it to execute it yourself ..... correct it until it works
0
 

Author Closing Comment

by:rgb192
ID: 31669638
thanks, now i can find int
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question