Skip to content

Instantly share code, notes, and snippets.

@JudsonMurray
Forked from marinoscar/udf_GetProcedureHash.sql
Created December 22, 2022 00:20
Show Gist options
  • Select an option

  • Save JudsonMurray/10b45dff551c4f83cb456bc79bf3120b to your computer and use it in GitHub Desktop.

Select an option

Save JudsonMurray/10b45dff551c4f83cb456bc79bf3120b to your computer and use it in GitHub Desktop.
Get's a MD5 hash string for a stored procedure definition. This helps to check the integrity of a stored procedure over time Raw
IF OBJECT_ID('udf_GetProcedureHash', 'FN') IS NOT NULL
BEGIN
DROP FUNCTION udf_GetProcedureHash
END
GO
CREATE FUNCTION udf_GetProcedureHash
(@ProcedureName as sysname)
RETURNS varchar(32)
AS
BEGIN
DECLARE @ProcedureText As varchar(max), @Result As varchar(32)
SET @ProcedureText = (SELECT OBJECT_DEFINITION (OBJECT_ID(@ProcedureName)))
SELECT @Result = CONVERT(VARCHAR(32), HashBytes('MD5', @ProcedureText), 2)
RETURN @Result
END
GO
/*
Test:
SELECT dbo.udf_GetProcedureHash(name) As HashValue, name As ProcedureName FROM sys.procedures;
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment