The following query returns all delete stored procedures with a delete clause;
SELECT
*
FROM
INFORMATION_SCHEMA.ROUTINES
WHERE
ROUTINE_TYPE = 'PROCEDURE'
AND ROUTINE_DEFINITION LIKE '%DELETE%'
I've started work as an SQL Server DBA.. there's a lot to learn, so i've created this blog to help me add generic examples I gain along the way, so I don't forget them.
The following query returns all delete stored procedures with a delete clause;
SELECT
*
FROM
INFORMATION_SCHEMA.ROUTINES
WHERE
ROUTINE_TYPE = 'PROCEDURE'
AND ROUTINE_DEFINITION LIKE '%DELETE%'
I noticed that when optimising a query it can be quite difficult. SQL server caches table information so it can be difficult to test how fast a query is running.
To clearr the cache, run the following query;
--Remove execution plan cache
DBCC FREEPROCCACHE
GO
--Remove data cache
DBCC DROPCLEANBUFFERS
GO
Please note, you should never do this on a live server!!
SELECT
OBJECT_NAME(object_id), * from sys.columns
where
name like '%col_name%';
I've been informed that using @@ERROR isn't the most robust way of error checking. An old example would be the following;
BEGIN TRANSACTION
EXEC sp_deleteuser 1
IF @@ERROR != 0 --RollBack Transaction if Error..
BEGIN
ROLLBACK TRANSACTION
END
ELSE
BEGIN
COMMIT TRANSACTION --finally, Commit the transaction if Success..
END
Instead of using the above example, you should be using the following;
BEGIN TRY
BEGIN TRANSACTION
EXEC sp_deleteuser 1
IF @@TRANCOUNT > 0
COMMIT TRANSACTION
END TRY
BEGIN CATCH
--Return error if any
SELECT
@errNo = ERROR_NUMBER()
, @errMsg = ERROR_MESSAGE()
, @errSeverity = ERROR_SEVERITY()
, @errState = ERROR_STATE()
IF @@TRANCOUNT > 0
ROLLBACK TRANSACTION
--Raise the error
RAISERROR (@errMsg, @errSeverity, @errState)
END CATCH