|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Friday, May 10, 2013 3:16 PM
Points: 263,
Visits: 269
|
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Wednesday, January 12, 2011 2:11 AM
Points: 1,
Visits: 37
|
|
I have a solution that I use to find occurrences of a string within any stored procedure in a database. Although is uses a cursor, I have found the performance to be more than acceptable. This solution also has the merit of telling you the line number that the match was found on.
CREATE proc dbo.zspFind @Search varchar(100) as
DECLARE cur CURSOR READ_ONLY FOR select name, syscomments.id, colid from syscomments join sysobjects on syscomments.id=sysobjects.id where xtype='P' and category=0 order by name, colid
DECLARE @name varchar(100) DECLARE @id int DECLARE @colid int DECLARE @buf varchar(8000) DECLARE @pos1 int DECLARE @pos2 int DECLARE @line varchar(4000) DECLARE @lineno int
declare @out table (Name varchar(100), line int, [text] varchar(1000))
OPEN cur
set @buf='' set @lineno=1
FETCH NEXT FROM cur INTO @name, @id, @colid WHILE (@@fetch_status <> -1) BEGIN
select @buf=@buf+cast([text] as varchar(4000)) from syscomments where id=@id and colid=@colid
set @pos1=1 select @pos2=charindex(char(10), @buf, @pos1)
while @pos2>0 begin select @line=substring(@buf, @pos1, @pos2-@pos1), @lineno=@lineno+1
if charindex(@Search, @line)>0 begin set @line =replace(@line, char(9), '') insert into @out values (@name, @lineno, @line) end
select @pos1=@pos2+1 select @pos2=charindex(char(10), @buf, @pos1) end
set @buf=substring(@buf, @pos1, 4000) FETCH NEXT FROM cur INTO @name, @id, @colid
if @colid=1 set @lineNo=1
END
CLOSE cur DEALLOCATE cur
select * from @out
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Yesterday @ 1:59 AM
Points: 1,126,
Visits: 924
|
|
Nice article  The solution I use is to populate a table overnight with the full syscomments text for each object, across multiple databases. This table can then be queried. We use an Access front-end for the querying, so we can return a list of matches and then drilldown for a further look into the full text. An addition I'd like to make is to highlight the search string in the objects, like Google does with search results. I do like the idea of the line numbering mentioned above too.
|
|
|
|
|
SSCertifiable
       
Group: Moderators
Last Login: Thursday, May 09, 2013 12:38 PM
Points: 6,462,
Visits: 1,384
|
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Friday, June 12, 2009 7:37 AM
Points: 125,
Visits: 115
|
|
I haven't tried it for this scenario, but I would think that you could call the new 2005 function OBJECT_DEFINITION on the sysobjects table, and then query that function to find the dependencies that you are looking for. OBJECT_DEFINITION returns a varchar(max), so it is searchable.
SELECT name FROM sys.objects WHERE OBJECT_DEFINITION(object_id) LIKE '%searchtext%'
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Saturday, July 14, 2007 4:04 AM
Points: 9,
Visits: 1
|
|
| I usually know the whereabouts of the code I'm searching for, but if I really had to find something in the code, I'd probably script everything to a file and search for the string with a regular expressions enabled text editor since I would probably anyway have problems finding words with white space and/or line breaks between them.
|
|
|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Friday, May 10, 2013 3:16 PM
Points: 263,
Visits: 269
|
|
| If you are using an access front end, you can use the rich text box control to display the text and highlight the results. I implemented a similar thing years ago. If you really want it to fly, you can edit the raw RTF, but the performance is just as acceptable using the built in methods of the control.
|
|
|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Friday, May 10, 2013 3:16 PM
Points: 263,
Visits: 269
|
|
OBJECT_DEFINITION sounds like a real winner! I hadn't heard of that function, it sounds perfect though!
Thanks for sharing this tip Tim! I guess I need to swot up some more on SQL 2005!
|
|
|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Friday, May 10, 2013 3:16 PM
Points: 263,
Visits: 269
|
|
| That's definitly a valid approach, and I like that your solution give you line numbers. I have often wanted to expand out the functionality of my search app to be more like grep. It would be great to be able to see the line(s) the string appears on, so you have the context, line numbers etc.
|
|
|
|
|
Say Hey Kid
      
Group: General Forum Members
Last Login: Wednesday, January 05, 2011 9:32 AM
Points: 694,
Visits: 89
|
|
If you're using SQL Server 2005, wouldn't you do better to search sys.sql_modules? That's the replacement for syscomments. The definition column holds all the sql definitions, and is nvarchar(max). I imagine the object_definition() function simply queries that view, so you should get better performance by searching the view directly rather than using the function.
Rick townsends.ca
|
|
|
|