﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / Discuss Content Posted by Robert Cary / Article Discussions / Article Discussions by Author  / Searching Syscomments Accurately / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Fri, 24 May 2013 13:11:55 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Searching Syscomments Accurately</title><link>http://www.sqlservercentral.com/Forums/Topic333815-329-1.aspx</link><description>I was searching for the 2005 replace for syscomments and found this thread. I see it's fairly old, but figured I'd post the solution I've just worked up this afternoon. It seems to be working out well for me--fast and accurate as far as I tell.You will need the use of a Numbers/Tally table:&lt;pre style="font-size: 12px;"&gt;&lt;font color="black"&gt;&lt;/font&gt;&lt;font color="blue"&gt;CREATE PROCEDURE &lt;/font&gt;&lt;font color="black"&gt;admin.pFindStringInCode   &lt;/font&gt;&lt;font color="#434343"&gt;@StringToFind &lt;/font&gt;&lt;font color="blue"&gt;nvarchar&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="black"&gt;50&lt;/font&gt;&lt;font color="gray"&gt;)&lt;/font&gt;&lt;font color="blue"&gt;ASSET &lt;/font&gt;&lt;font color="black"&gt;NOCOUNT &lt;/font&gt;&lt;font color="blue"&gt;ON&lt;/font&gt;&lt;font color="gray"&gt;;  &lt;/font&gt;&lt;font color="blue"&gt;SELECT      &lt;/font&gt;&lt;font color="black"&gt;s.[name] &lt;/font&gt;&lt;font color="blue"&gt;AS &lt;/font&gt;&lt;font color="black"&gt;SchemaName&lt;/font&gt;&lt;font color="gray"&gt;,    &lt;/font&gt;&lt;font color="black"&gt;o.[name] &lt;/font&gt;&lt;font color="blue"&gt;AS &lt;/font&gt;&lt;font color="black"&gt;ObjectName&lt;/font&gt;&lt;font color="gray"&gt;,    &lt;/font&gt;&lt;font color="magenta"&gt;CAST&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="black"&gt;DENSE_RANK&lt;/font&gt;&lt;font color="gray"&gt;() &lt;/font&gt;&lt;font color="blue"&gt;OVER &lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="blue"&gt;ORDER BY &lt;/font&gt;&lt;font color="black"&gt;s.[Name]&lt;/font&gt;&lt;font color="gray"&gt;, &lt;/font&gt;&lt;font color="black"&gt;o.[Name]&lt;/font&gt;&lt;font color="gray"&gt;) &lt;/font&gt;&lt;font color="blue"&gt;AS varchar&lt;/font&gt;&lt;font color="gray"&gt;) + &lt;/font&gt;&lt;font color="red"&gt;'.' &lt;/font&gt;&lt;font color="gray"&gt;+     &lt;/font&gt;&lt;font color="magenta"&gt;CAST&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="black"&gt;ROW_NUMBER&lt;/font&gt;&lt;font color="gray"&gt;() &lt;/font&gt;&lt;font color="blue"&gt;OVER &lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="black"&gt;PARTITION &lt;/font&gt;&lt;font color="blue"&gt;BY &lt;/font&gt;&lt;font color="black"&gt;s.[Name]&lt;/font&gt;&lt;font color="gray"&gt;, &lt;/font&gt;&lt;font color="black"&gt;o.[Name] &lt;/font&gt;&lt;font color="blue"&gt;ORDER BY &lt;/font&gt;&lt;font color="black"&gt;s.[Name]&lt;/font&gt;&lt;font color="gray"&gt;, &lt;/font&gt;&lt;font color="black"&gt;o.[Name]&lt;/font&gt;&lt;font color="gray"&gt;) &lt;/font&gt;&lt;font color="blue"&gt;AS varchar&lt;/font&gt;&lt;font color="gray"&gt;) &lt;/font&gt;&lt;font color="blue"&gt;AS &lt;/font&gt;&lt;font color="black"&gt;Instance&lt;/font&gt;&lt;font color="gray"&gt;,    &lt;/font&gt;&lt;font color="red"&gt;'...'&lt;/font&gt;&lt;font color="gray"&gt;+&lt;/font&gt;&lt;font color="magenta"&gt;SUBSTRING&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="black"&gt;m.definition&lt;/font&gt;&lt;font color="gray"&gt;, &lt;/font&gt;&lt;font color="blue"&gt;CHARINDEX&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="#434343"&gt;@StringToFind&lt;/font&gt;&lt;font color="gray"&gt;, &lt;/font&gt;&lt;font color="black"&gt;m.definition&lt;/font&gt;&lt;font color="gray"&gt;, &lt;/font&gt;&lt;font color="black"&gt;n.Number&lt;/font&gt;&lt;font color="gray"&gt;), &lt;/font&gt;&lt;font color="black"&gt;100&lt;/font&gt;&lt;font color="gray"&gt;)+&lt;/font&gt;&lt;font color="red"&gt;'...' &lt;/font&gt;&lt;font color="blue"&gt;AS &lt;/font&gt;&lt;font color="black"&gt;Snippet   &lt;/font&gt;&lt;font color="blue"&gt;FROM    &lt;/font&gt;&lt;font color="black"&gt;sys.sql_modules &lt;/font&gt;&lt;font color="blue"&gt;AS &lt;/font&gt;&lt;font color="black"&gt;m    &lt;/font&gt;&lt;font color="blue"&gt;JOIN &lt;/font&gt;&lt;font color="black"&gt;sys.objects &lt;/font&gt;&lt;font color="blue"&gt;AS &lt;/font&gt;&lt;font color="black"&gt;o &lt;/font&gt;&lt;font color="blue"&gt;ON &lt;/font&gt;&lt;font color="black"&gt;o.[object_id] &lt;/font&gt;&lt;font color="blue"&gt;= &lt;/font&gt;&lt;font color="black"&gt;m.[object_id]    &lt;/font&gt;&lt;font color="blue"&gt;JOIN &lt;/font&gt;&lt;font color="black"&gt;sys.schemas &lt;/font&gt;&lt;font color="blue"&gt;AS &lt;/font&gt;&lt;font color="black"&gt;s &lt;/font&gt;&lt;font color="blue"&gt;ON &lt;/font&gt;&lt;font color="black"&gt;o.[schema_id] &lt;/font&gt;&lt;font color="blue"&gt;= &lt;/font&gt;&lt;font color="black"&gt;s.[schema_id]    &lt;/font&gt;&lt;font color="gray"&gt;CROSS &lt;/font&gt;&lt;font color="blue"&gt;JOIN &lt;/font&gt;&lt;font color="black"&gt;admin.tbl_Numbers &lt;/font&gt;&lt;font color="blue"&gt;AS &lt;/font&gt;&lt;font color="black"&gt;n  &lt;/font&gt;&lt;font color="blue"&gt;WHERE    &lt;/font&gt;&lt;font color="black"&gt;n.Number &lt;/font&gt;&lt;font color="gray"&gt;&amp;lt; &lt;/font&gt;&lt;font color="magenta"&gt;LEN&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="black"&gt;m.definition&lt;/font&gt;&lt;font color="gray"&gt;)    AND &lt;/font&gt;&lt;font color="magenta"&gt;SUBSTRING&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="black"&gt;m.definition&lt;/font&gt;&lt;font color="gray"&gt;, &lt;/font&gt;&lt;font color="black"&gt;n.Number&lt;/font&gt;&lt;font color="gray"&gt;, &lt;/font&gt;&lt;font color="magenta"&gt;LEN&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="#434343"&gt;@StringToFind&lt;/font&gt;&lt;font color="gray"&gt;)) &lt;/font&gt;&lt;font color="blue"&gt;= &lt;/font&gt;&lt;font color="#434343"&gt;@StringToFind   &lt;/font&gt;&lt;font color="gray"&gt;AND &lt;/font&gt;&lt;font color="black"&gt;m.definition &lt;/font&gt;&lt;font color="gray"&gt;LIKE &lt;/font&gt;&lt;font color="red"&gt;N'%'&lt;/font&gt;&lt;font color="gray"&gt;+ &lt;/font&gt;&lt;font color="#434343"&gt;@StringToFind &lt;/font&gt;&lt;font color="gray"&gt;+ &lt;/font&gt;&lt;font color="red"&gt;N'%'   &lt;/font&gt;&lt;font color="blue"&gt;SET &lt;/font&gt;&lt;font color="black"&gt;NOCOUNT &lt;/font&gt;&lt;font color="blue"&gt;OFF&lt;/font&gt;&lt;font color="gray"&gt;;&lt;/font&gt;&lt;font color="black"&gt;GO&lt;/font&gt;&lt;/pre&gt;</description><pubDate>Fri, 12 Jun 2009 13:22:38 GMT</pubDate><dc:creator>Jason Hannas </dc:creator></item><item><title>RE: Searching Syscomments Accurately</title><link>http://www.sqlservercentral.com/Forums/Topic333815-329-1.aspx</link><description>I recently was examining source code and used a different technique.  I believe the following will accomplish the same thing in SQL 2005.  I do not know if it works in SQL 2000.SELECT OBJECT_NAME(object_ID) AS Name, definition   FROM sys.sql_modules  WHERE  definition LIKE '%YourSearchString%'</description><pubDate>Mon, 07 Jan 2008 11:40:54 GMT</pubDate><dc:creator>dtalsma</dc:creator></item><item><title>RE: Searching Syscomments Accurately</title><link>http://www.sqlservercentral.com/Forums/Topic333815-329-1.aspx</link><description>It looks like some of those group bys are unneccessary. Check out Jesper's code for the best 2k approach to date. As far as an explanation goes, This code evolved quite a bit, I never reviewed the code as a whole prior to posting. In fact, the 2k code was based on the same approach used in the 2k5 code. </description><pubDate>Thu, 25 Jan 2007 12:13:00 GMT</pubDate><dc:creator>Robert Michael Cary</dc:creator></item><item><title>RE: Searching Syscomments Accurately</title><link>http://www.sqlservercentral.com/Forums/Topic333815-329-1.aspx</link><description>Wow! That's much better! Nice work Jesper!</description><pubDate>Thu, 25 Jan 2007 12:11:00 GMT</pubDate><dc:creator>Robert Michael Cary</dc:creator></item><item><title>RE: Searching Syscomments Accurately</title><link>http://www.sqlservercentral.com/Forums/Topic333815-329-1.aspx</link><description>&lt;P&gt;I modified your last query a bit and obtained something that seems to be faster on SQL Server 2000:&lt;/P&gt;&lt;P&gt;SELECT distinct O.Name, O.TypeFROM( select s1.id, cast(coalesce(s1.text, '') as varchar(8000)) + cast(coalesce(s2.text, '') as varchar(8000)) as [text] from syscomments s1 left join syscomments s2 on s2.id = s1.id and s2.colid = s1.colid + 1) CINNER JOIN sysobjects O ON C.id = O.IdWHERE C.TEXT LIKE '%yourSearchString%'&lt;/P&gt;</description><pubDate>Thu, 25 Jan 2007 05:40:00 GMT</pubDate><dc:creator>Jesper-244176</dc:creator></item><item><title>RE: Searching Syscomments Accurately</title><link>http://www.sqlservercentral.com/Forums/Topic333815-329-1.aspx</link><description>Sure! Specifically what code are you refering to? The code in the article or the code in the forum (or both)</description><pubDate>Tue, 23 Jan 2007 12:43:00 GMT</pubDate><dc:creator>Robert Michael Cary</dc:creator></item><item><title>RE: Searching Syscomments Accurately</title><link>http://www.sqlservercentral.com/Forums/Topic333815-329-1.aspx</link><description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;In your code you use a lot of GROUP BY's --- can you explain why?&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;P&gt; &lt;/P&gt;</description><pubDate>Tue, 23 Jan 2007 07:24:00 GMT</pubDate><dc:creator>Jags2001</dc:creator></item><item><title>RE: Searching Syscomments Accurately</title><link>http://www.sqlservercentral.com/Forums/Topic333815-329-1.aspx</link><description>That will not work.  This returns only the first row of text in syscomments (2000).  No idea about 2005.</description><pubDate>Tue, 23 Jan 2007 06:18:00 GMT</pubDate><dc:creator>Ninja's_RGR'us</dc:creator></item><item><title>RE: Searching Syscomments Accurately</title><link>http://www.sqlservercentral.com/Forums/Topic333815-329-1.aspx</link><description>&lt;P&gt;Yep, that's what I was going to do, just not got round to it yet &lt;img src='images/emotions/smile.gif' height='20' width='20' border='0' title='Smile' align='absmiddle'&gt;&lt;/P&gt;&lt;P&gt; &lt;/P&gt;</description><pubDate>Tue, 23 Jan 2007 02:30:00 GMT</pubDate><dc:creator>Michael Lysons</dc:creator></item><item><title>RE: Searching Syscomments Accurately</title><link>http://www.sqlservercentral.com/Forums/Topic333815-329-1.aspx</link><description>I think this also works.SELECT ROUTINE_NAME, ROUTINE_DEFINITION             FROM INFORMATION_SCHEMA.ROUTINES             WHERE ROUTINE_DEFINITION LIKE '%some_text%'           AND ROUTINE_TYPE='PROCEDURE'</description><pubDate>Tue, 23 Jan 2007 02:00:00 GMT</pubDate><dc:creator>Jeremy Hoy-369361</dc:creator></item><item><title>RE: Searching Syscomments Accurately</title><link>http://www.sqlservercentral.com/Forums/Topic333815-329-1.aspx</link><description>This is also a good solution from Tim Chapman. He is using object_definition() function.http://www.sqlservercentral.com/articles/articlelink.asp?articleid=2809Andy</description><pubDate>Tue, 23 Jan 2007 00:09:00 GMT</pubDate><dc:creator>Ramseier Andreas</dc:creator></item><item><title>RE: Searching Syscomments Accurately</title><link>http://www.sqlservercentral.com/Forums/Topic333815-329-1.aspx</link><description>The 30 is an arbitary max of colid. If you have a proc greater than 120,000 bytes long, this will fail. Thank you for brining this up. I've thought about it and there are three options:1. Lookup the max colid value from syscomments before running the query (although this seems to kill the query plan)2. Use the maximum possible value (255) 3. Skip the numbers table and join syscomments to itself to generate your range of colId Values.Here is the code:&lt;pre&gt;DECLARE @max SMALLINTSELECT @max = max(COlid) FROM syscomments SELECT DISTINCT O.Name, O.Type    FROM    (        SELECT Id,            CAST(COALESCE(MIN(CASE WHEN sc.colId = Num-1 THEN sc.text END), '') AS VARCHAR(8000)) +            CAST(COALESCE(MIN(CASE WHEN sc.colId = Num THEN sc.text END), '') AS VARCHAR(8000)) AS [text]        FROM syscomments SC            INNER JOIN numbers N                ON N.Num = SC.colid                OR N.num-1 = SC.colid        WHERE N.Num &lt;= @max        GROUP BY id, Num    ) CINNER JOIN sysobjects O    ON C.id = O.IdWHERE C.TEXT LIKE '%yourSearchString%' SELECT DISTINCT O.Name, O.Type    FROM    (        SELECT Id,            CAST(COALESCE(MIN(CASE WHEN sc.colId = Num-1 THEN sc.text END), '') AS VARCHAR(8000)) +            CAST(COALESCE(MIN(CASE WHEN sc.colId = Num THEN sc.text END), '') AS VARCHAR(8000)) AS [text]        FROM syscomments SC            INNER JOIN numbers N                ON N.Num = SC.colid                OR N.num-1 = SC.colid        WHERE N.Num &lt;= 255        GROUP BY id, Num    ) CINNER JOIN sysobjects O    ON C.id = O.IdWHERE C.TEXT LIKE '%yourSearchString%'SELECT DISTINCT O.Name, O.Type FROM ( SELECT Id,  CAST(COALESCE(MIN(CASE WHEN sc.colId = Num-1 THEN sc.text END), '') AS VARCHAR(8000)) +  CAST(COALESCE(MIN(CASE WHEN sc.colId = Num THEN sc.text END), '') AS VARCHAR(8000)) AS [text]FROM syscomments SC INNER JOIN (SELECT TOP 100 PERCENT ColID AS Num FROM Syscomments GROUP BY ColID ORDER BY ColID DESC) N ON N.Num = SC.colid OR N.num-1 = SC.colid GROUP BY id, Num) CINNER JOIN sysobjects O ON C.id = O.Id WHERE C.TEXT LIKE '%yourSearchString%'&lt;/pre&gt;Overall, the last query--with the self-join--appears to be the fastest; it has fewer scans but more reads.I hope this all helps</description><pubDate>Mon, 22 Jan 2007 17:31:00 GMT</pubDate><dc:creator>Robert Michael Cary</dc:creator></item><item><title>RE: Searching Syscomments Accurately</title><link>http://www.sqlservercentral.com/Forums/Topic333815-329-1.aspx</link><description>&lt;P&gt;Robert,&lt;/P&gt;&lt;P&gt;Your solution works great ... I am however having a bit of a problem following the code - could you clarify the significance of the number 30 used in the WHERE clause?&lt;/P&gt;&lt;P&gt; &lt;/P&gt;&lt;P&gt;Thanks a lot&lt;/P&gt;</description><pubDate>Mon, 22 Jan 2007 14:58:00 GMT</pubDate><dc:creator>sql_er</dc:creator></item><item><title>RE: Searching Syscomments Accurately</title><link>http://www.sqlservercentral.com/Forums/Topic333815-329-1.aspx</link><description>Thank you to everyone who has commented on the article. I've really enjoyed hearing how others have approached this problem and it's also great to hear that there are better approaches available in SQL2k. I've learned a lot from you guys!</description><pubDate>Mon, 22 Jan 2007 14:06:00 GMT</pubDate><dc:creator>Robert Michael Cary</dc:creator></item><item><title>RE: Searching Syscomments Accurately</title><link>http://www.sqlservercentral.com/Forums/Topic333815-329-1.aspx</link><description>Maybe...but its probably negligible.  If you use sys.sql_modules, you still have to go lookup the name of the object.Tim</description><pubDate>Mon, 22 Jan 2007 14:01:00 GMT</pubDate><dc:creator>Tim Chapman-218780</dc:creator></item><item><title>RE: Searching Syscomments Accurately</title><link>http://www.sqlservercentral.com/Forums/Topic333815-329-1.aspx</link><description>&lt;P&gt;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).&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;</description><pubDate>Mon, 22 Jan 2007 13:52:00 GMT</pubDate><dc:creator>Rick Townsend</dc:creator></item><item><title>RE: Searching Syscomments Accurately</title><link>http://www.sqlservercentral.com/Forums/Topic333815-329-1.aspx</link><description>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. </description><pubDate>Mon, 22 Jan 2007 09:57:00 GMT</pubDate><dc:creator>Robert Michael Cary</dc:creator></item><item><title>RE: Searching Syscomments Accurately</title><link>http://www.sqlservercentral.com/Forums/Topic333815-329-1.aspx</link><description>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!</description><pubDate>Mon, 22 Jan 2007 09:54:00 GMT</pubDate><dc:creator>Robert Michael Cary</dc:creator></item><item><title>RE: Searching Syscomments Accurately</title><link>http://www.sqlservercentral.com/Forums/Topic333815-329-1.aspx</link><description>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. </description><pubDate>Mon, 22 Jan 2007 09:53:00 GMT</pubDate><dc:creator>Robert Michael Cary</dc:creator></item><item><title>RE: Searching Syscomments Accurately</title><link>http://www.sqlservercentral.com/Forums/Topic333815-329-1.aspx</link><description>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.</description><pubDate>Mon, 22 Jan 2007 09:18:00 GMT</pubDate><dc:creator>Ronald Green-301498</dc:creator></item><item><title>RE: Searching Syscomments Accurately</title><link>http://www.sqlservercentral.com/Forums/Topic333815-329-1.aspx</link><description>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.objectsWHERE OBJECT_DEFINITION(object_id) LIKE '%searchtext%'</description><pubDate>Mon, 22 Jan 2007 06:51:00 GMT</pubDate><dc:creator>Tim Chapman-218780</dc:creator></item><item><title>RE: Searching Syscomments Accurately</title><link>http://www.sqlservercentral.com/Forums/Topic333815-329-1.aspx</link><description>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.</description><pubDate>Mon, 22 Jan 2007 06:20:00 GMT</pubDate><dc:creator>Andy Warren</dc:creator></item><item><title>RE: Searching Syscomments Accurately</title><link>http://www.sqlservercentral.com/Forums/Topic333815-329-1.aspx</link><description>&lt;P&gt;Nice article &lt;img src='images/emotions/smile.gif' height='20' width='20' border='0' title='Smile' align='absmiddle'&gt;&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;</description><pubDate>Mon, 22 Jan 2007 03:36:00 GMT</pubDate><dc:creator>Michael Lysons</dc:creator></item><item><title>RE: Searching Syscomments Accurately</title><link>http://www.sqlservercentral.com/Forums/Topic333815-329-1.aspx</link><description>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)asDECLARE cur CURSORREAD_ONLYFOR select name, syscomments.id, colid	from syscomments 	join sysobjects on syscomments.id=sysobjects.id	where xtype='P' and category=0	order by name, colidDECLARE @name varchar(100)DECLARE @id intDECLARE @colid intDECLARE @buf varchar(8000)DECLARE @pos1 intDECLARE @pos2 intDECLARE @line varchar(4000)DECLARE @lineno intdeclare @out table (Name varchar(100), line int, [text] varchar(1000)) OPEN curset @buf=''set @lineno=1FETCH NEXT FROM cur INTO @name, @id, @colidWHILE (@@fetch_status &lt;&gt; -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&gt;0	begin				select @line=substring(@buf, @pos1, @pos2-@pos1), @lineno=@lineno+1		if charindex(@Search, @line)&gt;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=1ENDCLOSE curDEALLOCATE curselect * from @out</description><pubDate>Mon, 22 Jan 2007 03:20:00 GMT</pubDate><dc:creator>Hugh Macmillan-281005</dc:creator></item><item><title>Searching Syscomments Accurately</title><link>http://www.sqlservercentral.com/Forums/Topic333815-329-1.aspx</link><description>Comments posted here are about the content posted at &lt;A HREF="http://www.sqlservercentral.com/columnists/rcary/2792.asp"&gt;http://www.sqlservercentral.com/columnists/rcary/2792.asp&lt;/A&gt;</description><pubDate>Tue, 02 Jan 2007 11:55:00 GMT</pubDate><dc:creator>Robert Michael Cary</dc:creator></item></channel></rss>