Skip to content

Instantly share code, notes, and snippets.

@dalenewman
Created July 28, 2016 14:34
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dalenewman/d153485c0ac21ca6e158497ca1672e80 to your computer and use it in GitHub Desktop.
Save dalenewman/d153485c0ac21ca6e158497ca1672e80 to your computer and use it in GitHub Desktop.
sp_log
USE master
GO
-- you have to start procedure name with sp_ to make it global
CREATE PROCEDURE sp_log
@Message NVARCHAR(512),
@RowCount INT = null OUTPUT,
@Delimiter NCHAR(1) = N' ',
@PadChar NCHAR(1) = N'-'
AS
BEGIN
SET @RowCount = @@ROWCOUNT;
DECLARE @LogDate AS NVARCHAR(50);
DECLARE @RowCountPadded AS NCHAR(8);
SET @LogDate = CONVERT(NVARCHAR(50),GETDATE(),121);
SELECT @RowCountPadded = CASE @RowCount WHEN 0 THEN REPLICATE(@PadChar,8) ELSE REPLACE(STR(@RowCount, 8), SPACE(1), @PadChar) END;
SET @Message = @LogDate + @Delimiter + @RowCountPadded + @Delimiter + @Message;
RAISERROR (@Message, 0, 1) WITH NOWAIT;
END
GO
-- Mark it as system object
EXEC sys.sp_MS_marksystemobject sp_log
GO
@JohnMegens-SMC
Copy link

Dont forget to: GRANT EXECUTE ON OBJECT::[master].dbo.sp_log to public;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment