As part of some enhancement, a new column has been added to over 750 tables. Now my problem is within almost 3000+ of stored procedures, some old INSERTs do not name columns.
Is there any way I can get a "list" of all the stored procedures that are inserting without column names?
I have tried:
SELECT OBJECT_NAME(object_id)
FROM sys.sql_modules
WHERE definition LIKE '%insert into%'
AND definition NOT LIKE '%) value%'
AND definition NOT LIKE '%)%' + CHAR(10) + '%value%'
AND definition NOT LIKE '%)%' + CHAR(13) + '%value%'
AND definition LIKE '%value%'
But it's still misses a lot.
P.S. – The above query is returning Insert Into (ColumnNames) SELECT scenario too , which i don't want.
Best Answer
There is a DMV called sys.dm_sql_referenced_entities. It returns a column
is_insert_all
documented thusThis seems to be what you're looking for.
Here's an example of it in practice. First we'll create a simple one-column table
Now two stored procedures, one references columns explicitly the other does not.
The DMV is a table-valued function. As such it can be invoked using the CROSS APPLY syntax. The names of procedures to pass to this function can be obtained from the catalog view sys.procedures. The documentation for this DMV states
We can get the schema name using a built-in metadata function handily called SCHEMA_NAME(). Putting it all together we get
Which returns this