Last active
September 1, 2020 16:33
-
-
Save rvegajr/14e85b12c011f0bc67a08ec3fa2c913b to your computer and use it in GitHub Desktop.
A few templated code for MSSQL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- * * * * * * * * Print command alternative * * * * * * * * * * * | |
DECLARE @NOTE VARCHAR(8000) = ''; | |
SET @NOTE = 'Deleted records ' + CONVERT(VARCHAR, @@ROWCOUNT); RAISERROR (@NOTE, 10, 1) WITH NOWAIT; | |
-- * * * * * * * * Try..Catch.. Print command alternative * * * * * * * * | |
--BEGIN | |
SET NOCOUNT ON; | |
DECLARE @PMSG VARCHAR(8000) = ''; DECLARE @PROC VARCHAR(512) = 'SAMPLE: '; | |
BEGIN TRY | |
SELECT 'TEST' AS MSG INTO #TMP; | |
SET @PMSG = 'Returned Rows ' + CONVERT(VARCHAR(255), @@ROWCOUNT); RAISERROR(@PMSG, 10, 1) WITH NOWAIT; | |
SELECT * FROM #TMP; | |
THROW 51000, 'Sample Exception.. catastophic error!', 1; | |
END TRY | |
BEGIN CATCH | |
DECLARE @ErrorMessage NVARCHAR(4000); DECLARE @ErrorSeverity INT; DECLARE @ErrorState INT; DECLARE @ERROR_LINE INT; | |
SELECT | |
@ErrorMessage = 'ERROR! ' + ERROR_MESSAGE() + ' Line='+CONVERT(VARCHAR, ERROR_LINE()), | |
@ErrorSeverity = ERROR_SEVERITY(), @ErrorState = ERROR_STATE(), @ERROR_LINE = ERROR_LINE(); | |
SET @PMSG = @ErrorMessage + ' Severity=' + CONVERT(VARCHAR(255), @ErrorSeverity) + | |
', State=' + CONVERT(VARCHAR(255), @ErrorState); RAISERROR(@PMSG, 10, 1) WITH NOWAIT; | |
RAISERROR(@ErrorMessage, @ErrorSeverity, @ErrorState); | |
END CATCH | |
DROP TABLE IF EXISTS #TMP; | |
--END |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment