Question : sql server search query

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,
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
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

Answer : sql server search query

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
Random Solutions  
 
programming4us programming4us