Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 123»»»

Searching Syscomments Accurately Expand / Collapse
Author
Message
Posted Tuesday, January 02, 2007 11:55 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Tuesday, April 01, 2014 11:20 AM
Points: 263, Visits: 275
Comments posted here are about the content posted at http://www.sqlservercentral.com/columnists/rcary/2792.asp

SQL guy and Houston Magician
Post #333815
Posted Monday, January 22, 2007 3:20 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, February 27, 2014 2:26 AM
Points: 1, Visits: 41
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
Post #338598
Posted Monday, January 22, 2007 3:36 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Friday, April 11, 2014 1:38 AM
Points: 1,144, Visits: 1,050

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.

Post #338599
Posted Monday, January 22, 2007 6:20 AM
SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: Moderators
Last Login: Today @ 9:01 AM
Points: 6,705, Visits: 1,680
Nice work. I knew a proc could get split across multiple rows, just never ran into the scenario you describe where the word is split (or I ran into and didnt notice it!). Creating the super select as a view might come in handy.

Andy
SQLAndy - My Blog!
Connect with me on LinkedIn
Follow me on Twitter
Post #338630
Posted Monday, January 22, 2007 6:51 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-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%'
Post #338639
Posted Monday, January 22, 2007 9:18 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum 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.
Post #338708
Posted Monday, January 22, 2007 9:53 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Tuesday, April 01, 2014 11:20 AM
Points: 263, Visits: 275
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.

SQL guy and Houston Magician
Post #338724
Posted Monday, January 22, 2007 9:54 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Tuesday, April 01, 2014 11:20 AM
Points: 263, Visits: 275
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!


SQL guy and Houston Magician
Post #338725
Posted Monday, January 22, 2007 9:57 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Tuesday, April 01, 2014 11:20 AM
Points: 263, Visits: 275
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.

SQL guy and Houston Magician
Post #338726
Posted Monday, January 22, 2007 1:52 PM
Say Hey Kid

Say Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay 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
Post #338796
« Prev Topic | Next Topic »

Add to briefcase 123»»»

Permissions Expand / Collapse