Hope this query will help you to find the SPs having INSERT
statement without specifying columns in it.
First and second steps have sample table and stored procedure.
Executing 3rd and 4th steps together returns the SPs.
1.-----------Sample Table
CREATE TABLE Tbl_Employee
(
EmpId int identity(1,1),
EmpName varchar(16)
);
2.-----------Sample Stored Procedures
CREATE PROCEDURE Prc_Without_Columns_VALUES
AS
BEGIN
INSERT INTO Tbl_Employee
VALUES('Rajesh Ranjan');
END
CREATE PROCEDURE Prc_Without_Columns_SELECT
AS
BEGIN
INSERT INTO Tbl_Employee
SELECT TOP 1 EmpName FROM TblEmployee
END
CREATE PROCEDURE Prc_With_Columns_VALUES
AS
BEGIN
INSERT INTO Tbl_Employee(EmpName)
VALUES('Alok Kuwnar')
END
CREATE PROCEDURE Prc_With_Columns_SELECT
AS
BEGIN
INSERT INTO Tbl_Employee(EmpName)
SELECT'Aaron Bertrand'
END
--3---------Take object definition (REPLACE space, tab and enter with '') into temporary table (except all system objects)
DROP TABLE IF EXISTS #SpDefinition
CREATE TABLE #SpDefinition
(
SPName VARCHAR(128),
Definition NVARCHAR(4000)
)
SET NOCOUNT ON
INSERT INTO #SpDefinition(SPName,Definition)
SELECT
OBJECT_NAME(SM.OBJECT_ID) SPName
,REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(SM.DEFINITION,
'"',''), --Removing double quote
']',''), --Removing closing square bracket
'[',''), --Removing oppenign square bracket
' ',''), --Removing space
' ',''), --Removing Tab
CHAR(13),''), --Removing enter
CHAR(10),'') Definition
FROM
SYS.SQL_MODULES SM
INNER JOIN SYS.OBJECTS O
ON O.Object_id=SM.Object_id
WHERE
O.Schema_Id NOT IN (2,3,4,16384,16385,16386,16387,16389,16390,16391,16392,16393)
--4---------Find the stored procedures not using column names within INSERT statement
DECLARE @StoredProcedures TABLE ([Name] VARCHAR(128))
DECLARE @SchemaName VARCHAR(24)
DECLARE @TableName NVARCHAR(128)
DECLARE @SearchValue1 NVARCHAR(256)
DECLARE @SearchValue2 NVARCHAR(256)
DECLARE @SearchValue3 NVARCHAR(256)
DECLARE @SearchValue4 NVARCHAR(256)
DECLARE Cur_GetTable CURSOR FOR SELECT SCHEMA_NAME(SCHEMA_ID), SchemaName,REPLACE([Name],' ','') TableName FROM SYS.OBJECTS WHERE [TYPE] IN ('U','V') --For user defined objects only
OPEN Cur_GetTable
FETCH NEXT FROM Cur_GetTable INTO @SchemaName,@TableName
WHILE @@FETCH_STATUS=0
BEGIN
SET @SearchValue1 = '%INSERTINTO'+@TableName+'VALUES%' --Without Schema
SET @SearchValue2 = '%INSERTINTO'+@TableName+'SELECT%' --Without Schema
SET @SearchValue3 = '%INSERTINTO'+@SchemaName+'.'+@TableName+'VALUES%' --With Schema
SET @SearchValue4 = '%INSERTINTO'+@SchemaName+'.'+@TableName+'SELECT%' --With Schema
INSERT INTO @StoredProcedures([Name])
SELECT SPName FROM #SpDefinition
WHERE
(Definition LIKE @SearchValue1
OR Definition LIKE @SearchValue2
OR Definition LIKE @SearchValue3
OR Definition LIKE @SearchValue4
)
FETCH NEXT FROM Cur_GetTable INTO @SchemaName,@TableName
END
CLOSE Cur_GetTable
DEALLOCATE Cur_GetTable
SET NOCOUNT OFF
SELECT [Name] FROM @StoredProcedures
Results to
----------------------------
Prc_Without_Columns_VALUES
Prc_Without_Columns_SELECT
(2 rows affected)
Thanks!
INSERT INTO TABLENAME SELECT FROM TABLE
VALUES
, notVALUE
, by the way.