identify insert stored proc - sql

How do I identify stored proc that inserts records into "TableXYZ"?
I could have hundreds of stored procs.
Please help.

Use:
SELECT OBJECT_NAME(m.object_id), m.*
FROM SYS.SQL_MODULES m
WHERE m.definition like N'%INSERT INTO my_table_name%'
This will allow you to search for table reference(s).
Background:
SYSCOMMENTSand INFORMATION_SCHEMA.routines have NVARCHAR(4000) columns, while SYS.SQL_MODULES definition column is NVARCHAR(MAX). So if "my_table_name" is used at position 3998, it won't be found. SYSCOMMENTS does have multiple lines, but ROUTINES truncates.

I believe that you should be able to view the dependencies of the table by right clicking it on the management studio. This should at the very lease show you all stored procedures working with that table.

SELECT * FROM sys.sql_modules
WHERE definition like '%insert%TableXYZ%'
...gets you on the road to an answer.

SELECT ROUTINE_NAME, ROUTINE_DEFINITION
FROM INFORMATION_SCHEMA.ROUTINES
WHERE ROUTINE_DEFINITION LIKE '%insert into dbo.tablexyz%'
AND ROUTINE_TYPE='PROCEDURE'
You will need to adjust the like clause.

The add-in RedGate SQL Search, currently free, can be very helpful as it lets you search all of the code in your database. You could search for just the table name and see if that gets you close enough. If you consistently wrote your INSERT statement to look like INSERT INTO TableName, you could search for that entire phrase.
To do this kind of search manually as an T-SQL query, you can query the definition column in the sys.sql_modules system view. This provides you a lot of flexibility as you can use LIKE and/or PATINDEX to search for wildcards/patterns.

Related

What DML operations a table is performing inside Stored Procedure or SQL Functions

I am using SQL Server 2014. I have some user defined tables like Customer, PurchaseOrder, User, and so on. I am using those tables inside many stored procedures. In some cases, those stored procedures are almost 1000/1500 lines long.
Now I want to find out what operation(s) (insert/update/delete) those tables are doing inside every stored procedures.
I am doing it manually. But it is hell lot of effort. Besides, in manual effort, I might miss anything.
Can we write a SQL query by which without opening a stored procedure I can know what operation (insert/update/delete) a certain table is performing inside it.
Thanks in advance.
Based on your requirements you may find the following useful. You can search the complete text of all procedures / functions / triggers / views etc and look for matching key words.
select Schema_Name(o.schema_id)[schema], o.[name], o.type_desc
from sys.sql_modules m
join sys.objects o on o.object_id=m.object_id
where
m.definition like '%insert%customers%' or
m.definition like '%update%customers%' or
m.definition like '%delete%customers%'
order by type_desc, name
This can help you narrow down and identify potential objects. This in itself is not precise since it may find a procedure where you update orders and then use customers in a from or join subsequently.
If you have conventions you can rely on such as a delete will always be delete from customers and not delete customers or delete from c from... then you can of course improve the matching to increase the relevance of what you find.
A tool such as Redgate's SQLPrompt is invaluable here as you can script out all your procedure names prefixed with exec , paste it into SSMS and immediately preview the entire procedure code of each in a pop-up window.

In db2 how to find all the Stored Procedures having a given text in it

I want to find if a table is being used anywhere in all the stored procedures in a system.
Is there a query to fetch all the details of SP.
You can use SYSCAT.TABDEP and SYSCAT.ROUTINEDEP system catalog views.
For tables in Dynamic SQL statements, that are built and executed on the fly, you can use
select routinename,text from syscat.routines where language='SQL' and locate('<table-name>',text)>0
HTH
Sathyaram
The accepted answer didn't work for me for our particular flavor of DB2, but it set me in the right direction. Here is the query I wrote which allowed me to search sprocs in a given schema:
SELECT ROUTINE_NAME, ROUTINE_DEFINITION FROM sysibm.routines
WHERE SPECIFIC_SCHEMA='<YourSchemaName>'
AND ROUTINE_DEFINITION LIKE '<YourSearchText>%'
Replace YourSchemaName and YourSearchText with appropriate values.

Where does SQL Server store the stored procedure code?

I once needed the lines of the stored procedures to be able to trace whether I have a reference to some function, procedure or table, or sometimes to try to find something inside of the sp's code.
Where does SQL Server store the procedure's code?
Use sys.sql_modules because definition is nvarchar(max) because it will not truncate long code.
In INFORMATION_SCHEMA.ROUTINES the ROUTINE_DEFINITION column is only nvarchar(4000) so if you try view the text of a long procedure and you will see that it is truncated.
Use this to search for text in any procedure, view, function:
SELECT DISTINCT
o.name AS Object_Name,o.type_desc
FROM sys.sql_modules m
INNER JOIN sys.objects o ON m.object_id=o.object_id
WHERE m.definition Like '%'+#Search+'%'
ORDER BY o.type_desc,o.name
use this to view the text of a given procedure, view, function:
select * from sys.sql_modules where object_id=object_id('YourProcedure')
You can use
select object_definition(object_id(routine_name)) from information_schema.routines
or
select object_definition(object_id) from sys.objects where name = 'foo'
or even
select object_definition(object_id('foo')); -- 'foo' is the table name
These versions are never truncated.
It stored it inside the system schema tables:
SELECT * FROM INFORMATION_SCHEMA.ROUTINES
See MSDN about the INFORMATION_SCHEMA.ROUTINES view:
ms-help://MS.SQLCC.v10/MS.SQLSVR.v10.en/s10de_6tsql/html/c75561b2-c9a1-48a1-9afa-a5896b6454cf.htm
For a content search on this, you can do the follwing:
SELECT * FROM INFORMATION_SCHEMA.ROUTINES
WHERE ROUTINE_DEFINITION LIKE '%search_string%'
If you are just trying to view the stored procedures code you go into the progammabiltity folder within your DB and they should be all stored in there under stored procedures.
If you are trying to search for references to other objects, then you can run a query like this:
SELECT *
FROM syscomments
WHERE TEXT LIKE '%searchstring%'
This will return any objects in the database that reference the search string. You can then look at these objects to see what stored procedures (and views and functions) are doing so.
View Dependencies
In SQL Server Management Studio, right-click on a table, and choose "View Dependencies". You will see every object that references the table
INFORMATION_SCHEMA
The actual code for a stored proc, view, constraint, etc is stored in SysComments. You should query this using the views provided in the schema Information_Schema. Here are all the components of the Information_Schema.
If you use SQL Server Management Studion, you can right click on the database you want, then click "Tasks -> Generate Scripts".
There you can generate a script with all the SP's in one single file, separated files, or directly to a query window, and search/change what you want.
Hope this helps.
(this is for SQL Server 2008, but i think 2005 has this functionality too)
EDIT:
You can also see one single SP code, by following this path "YourDB -> Programmability -> Stored Procedures", then right click on the SP you want to see, and click "Modify", and a query window is opened with the code.

SQL Server 2005 search views for certain database objects

Is there a way in SQL Server to list all the views within a database that join from a particular object?
ie: find all the views that join from the table myTable
You can use sys.sql_dependencies:
select object_name(object_id),*
from sys.sql_dependencies
where referenced_major_id = object_id('<tablename>');
This will list all objects that depend on your table, you can restrict this to views by joining against sys.views:
select v.*
from sys.sql_dependencies d
join sys.views v on d.object_id = v.object_id
where referenced_major_id = object_id('<tablename>');
You have to search code and you have two options only. See comments below why other methods are not reliable.
select
object_name(m.object_id), m.*
from
sys.sql_modules m
where
m.definition like N'%my_view_name%'
or use OBJECT_DEFINITION
syscomments and INFORMATION_SCHEMA have nvarchar(4000) columns. So if "myViewName" is used at position 3998, it won't be found. syscomments does have multiple lines but ROUTINES truncates.
In SQL Server 2000, the sys.depends was unreliable, which affected "View dependencies" in the menus. I don't know if it's better in SQL 2005+. Example: view uses table, table is dropped and recreated, has different objectid, dependency = broken.
In SQL Server 2005, you can use a combination of sys.sql_dependencies and brute force parsing of the object text (as in gbn's answer). For more info on SQL Server 2005 dependencies, see http://msdn.microsoft.com/en-us/library/ms345449(SQL.90).aspx
In SQL Server 2008, there are new dependency DMVs and catalog views that are a bit more trustworthy than previous methods (sys.dm_sql_referenced_entities / sys.dm_sql_referencing_entities / sys.sql_expression_dependencies), but it is still easy to break them.
See this article for some ideas to make this work better. Also see http://msdn.microsoft.com/en-us/library/bb677168.aspx
Here's some of the examples from the link provided by unknown (in case that site disappears before stackoverflow does)
For SQL Server 2005, right click on the table name and select "View Dependencies"
A couple of the SQL only methods mentioned for SQL Server:
SELECT routine_name, routine_type FROM INFORMATION_SCHEMA.ROUTINES
WHERE ROUTINE_DEFINITION LIKE '%Employee%'
EXEC sp_depends #objname = N'HumanResources.Employee' ;

SQL 2005 - Search stored procedures for text (Not all text is being searched)

The following bits of code do not seem to be searching the entire routine definition.
Code block 1:
select top 50 * from information_schema.routines
where routine_definition like '%09/01/2008%' and specific_Name like '%NET'
Code Block 2:
SELECT ROUTINE_NAME, ROUTINE_DEFINITION FROM INFORMATION_SCHEMA.ROUTINES
WHERE ROUTINE_DEFINITION LIKE '%EffectiveDate%' AND ROUTINE_TYPE='PROCEDURE' and ROUTINE_NAME like '%NET'
I know for a fact that these bits of SQL work under most circumstances. The problem is this: When I run this for "EffectiveDate" which is buried at line ~800 in a few stored procedures, these stored procedures never show up in the results. It's as if "like" only searches so deep.
Any tips on fixing this?
I want to search the ENTIRE stored procedure for the specified text.
Thanks!
select *
FROM INFORMATION_SCHEMA.ROUTINES
WHERE OBJECTPROPERTY(OBJECT_ID(SPECIFIC_NAME),'IsMSShipped') =0
and OBJECT_DEFINITION(OBJECT_ID(SPECIFIC_NAME)) like '%EffectiveDate%'
AND ROUTINE_TYPE='PROCEDURE'
AND ROUTINE_NAME like '%NET'
Use the object definition instead.
Just to clarify why the procedure text is truncated:
ROUTINE_DEFINITION nvarchar(4000)
Returns the first 4000 characters of
the definition text of the function or
stored procedure if the function or
stored procedure is not encrypted.
Otherwise, returns NULL.
To ensure that you obtain the complete
definition, query the
OBJECT_DEFINITION function or the
definition column in the
sys.sql_modules catalog view.
Here's different approach if you are looking for a search feature against the SQL server databases. Basically the freely available search tool from RedGate.
Free 'Search' from Redgate
very fast,live and handy
Direct link
http://www.red-gate.com/products/SQL_Search/index.htm