views:

87

answers:

7

I need to find an insert statement into a specific table. Unfortunately, the application isn't mine to begin with, there are over 100 stored procedures in the database, I've stepped through the code writing down every stored procedure name from begining to end of this process and I still cannot find it. I know that the process has to occur some where because another process that depends on it works properly. I just cant find it.

Is there some sort TSQL that would allow me to search across multiple stored procedures for a specific line of text... more specifically "INSERT INTO [tablename]"

UPDATE

I've tried using:

 SELECT routine_name, routine_type  
 FROM INFORMATION_SCHEMA.ROUTINES  
 WHERE ROUTINE_DEFINITION LIKE '%INSERT INTO [tablename]%' 

Straight out, replacing [tablename] with the name of my table. This returns 0 rows.

I DO have a procedure with that exact line in it, and I would have assumed that it would have been returned in the above query, but its not bringing back anything at all.

UPDATE #2

After using the redgate sql search tool (using INSERT INTO [tablename]) it returns 1 sp, but i'm still in denial that this is correct due to some of the tsql (at the end of the sp its dropping tables that are still in the database...) (this is where my in-experience at sql shines through)

I still cant find where this is being called from though...

UPDATE #3

I just did a search on the entire solution for 'INSERT INTO [tablename]' to see if they maybe DIDN'T use a stored procedure for this call, and nothing... didn't find it anywhere.

+1  A: 
 SELECT routine_name, routine_type 
 FROM INFORMATION_SCHEMA.ROUTINES 
 WHERE ROUTINE_DEFINITION LIKE '%INSERT INTO [tablename]%'

warning: this may take a while. If your system is in heavy production use, don't run it on the entire table, but add some extra condition to search only a bunch of stored routines at a time

EDIT:

Perhaps you should try something else. Make a text dump of all routines, and use grep to search that. I am sure you can get a grep for windows. Dumping is easily done with management studio: navigate to the database, right click, and choose tasks > generate scripts.

Roland Bouman
Be aware that you'll need escape the square brackets if you use them, something like: LIKE '%INSERT INTO |[tablename|]%' ESCAPE '|'. Unless the square brackets are meant to just highlight it as being a placeholder for the real table name :)
AdaTheDev
Oddly enough its not even returning the 1 procedure that I WAS able to find that had the call in it. Unfortunately that sp isn't being called during this procedure.
Patrick
+1  A: 

Try

SELECT * FROM sys.syscomments
WHERE text LIKE '%CREATE PROCEDURE%'
AND text LIKE '%INSERT INTO%'

or

SELECT ROUTINE_NAME, ROUTINE_DEFINITION 
FROM INFORMATION_SCHEMA.ROUTINES 
WHERE ROUTINE_DEFINITION LIKE '%INSERT INTO%' 
AND ROUTINE_TYPE='PROCEDURE'

2nd one found here

rosscj2533
Tried both and neither worked....
Patrick
You mean they returned 0 rows?
rosscj2533
Correct... they "worked"... but didn't return what I needed so they didn't "work" for me ><
Patrick
+1  A: 

Just to add to the previous answers which I won't repeat, I think it's a good idea to have all the database objects scripted (and kept in source control). So I'd add the suggestion that you kill 2 birds with one stone - script the database, then search the generated sql scripts.

AdaTheDev
+1  A: 

Make a stored procedure out of it too!

GO

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO 

/*
exec sp_find_procs_containing 'insert into tablename'
*/

CREATE PROCEDURE [dbo].[sp_find_procs_containing]    
@search VARCHAR(100) = ''
AS
SET @search = '%' + @search + '%'
SELECT         ROUTINE_NAME,    ROUTINE_DEFINITION
FROM        
INFORMATION_SCHEMA.ROUTINES
WHERE        
ROUTINE_DEFINITION LIKE @search
ORDER BY ROUTINE_NAME

GO
SirDemon
+1  A: 

Have a look at this red-gate tool: sql search Still in beta but it ought to work

Conrad
awesome tool, tyvm it works at returning information pretty quickly.
Patrick
+1  A: 

You probably have some LF, CR/LFs, or extra spaces in there that are preventing your search from working. Reduce your search string until you get a match (e.g., try just using the table name), or change it to: %INSERT%SOMETHING%INTO%SOMETHING%, etc.

RedFilter
It finds 1 instance of the table name... the SELECT that pulls the information back out of it, but it doesn't find the INSERT that is putting the information into it.
Patrick
How do you know the `INSERT` statement is within the database and not coming from an application accessing the database?
RedFilter
There are no other applications accessing the database, other than the application I am working on. All of the sql for this process is contained in stored procedures.
Patrick
How are you determining that this statement is being run?
RedFilter
a procedure that happens after this process cannot run unless there is data in this table.
Patrick
+1  A: 

While the application is running, try running Profiler to capture the queries executed. Events to watch are SQL:BatchCompleted, RPC:Completed, and SP:StmtCompleted.

Also note that the "INTO" word is optional for the INSERT statement so also search for just "INSERT [tablename]".

DyingCactus
it turns out that it was running a COMPLETELY different process that was actually doing the calculations... even stepping through it i'm not sure how it works from the .net side... im thinking that it has something to do with a sub report that is linked directly to the stored procedure in the database... i updated the OTHER process and its working now.
Patrick