Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Searching Syscomments Accurately


Searching Syscomments Accurately

Author
Message
R Michael
R Michael
Old Hand
Old Hand (303 reputation)Old Hand (303 reputation)Old Hand (303 reputation)Old Hand (303 reputation)Old Hand (303 reputation)Old Hand (303 reputation)Old Hand (303 reputation)Old Hand (303 reputation)

Group: General Forum Members
Points: 303 Visits: 275
Comments posted here are about the content posted at http://www.sqlservercentral.com/columnists/rcary/2792.asp

SQL guy and Houston Magician
hugh@mip.co.uk
hugh@mip.co.uk
Forum Newbie
Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: General Forum Members
Points: 1 Visits: 43
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
Michael Lysons
Michael Lysons
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1342 Visits: 1405

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.


Andy Warren
Andy Warren
SSCrazy Eights
SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)

Group: Moderators
Points: 8194 Visits: 2711
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
Tim Chapman-218780
Tim Chapman-218780
SSC-Enthusiastic
SSC-Enthusiastic (143 reputation)SSC-Enthusiastic (143 reputation)SSC-Enthusiastic (143 reputation)SSC-Enthusiastic (143 reputation)SSC-Enthusiastic (143 reputation)SSC-Enthusiastic (143 reputation)SSC-Enthusiastic (143 reputation)SSC-Enthusiastic (143 reputation)

Group: General Forum Members
Points: 143 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%'
Ronald Green-301498
Ronald Green-301498
Forum Newbie
Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)

Group: General Forum Members
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.
R Michael
R Michael
Old Hand
Old Hand (303 reputation)Old Hand (303 reputation)Old Hand (303 reputation)Old Hand (303 reputation)Old Hand (303 reputation)Old Hand (303 reputation)Old Hand (303 reputation)Old Hand (303 reputation)

Group: General Forum Members
Points: 303 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
R Michael
R Michael
Old Hand
Old Hand (303 reputation)Old Hand (303 reputation)Old Hand (303 reputation)Old Hand (303 reputation)Old Hand (303 reputation)Old Hand (303 reputation)Old Hand (303 reputation)Old Hand (303 reputation)

Group: General Forum Members
Points: 303 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
R Michael
R Michael
Old Hand
Old Hand (303 reputation)Old Hand (303 reputation)Old Hand (303 reputation)Old Hand (303 reputation)Old Hand (303 reputation)Old Hand (303 reputation)Old Hand (303 reputation)Old Hand (303 reputation)

Group: General Forum Members
Points: 303 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
Rick Townsend
Rick Townsend
Say Hey Kid
Say Hey Kid (694 reputation)Say Hey Kid (694 reputation)Say Hey Kid (694 reputation)Say Hey Kid (694 reputation)Say Hey Kid (694 reputation)Say Hey Kid (694 reputation)Say Hey Kid (694 reputation)Say Hey Kid (694 reputation)

Group: General Forum Members
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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search