dbo.IsValidSQL

Here’s a handy stored procedure I found that validates dynamic SQL for SQL Server.

IF OBJECT_ID (‘dbo.IsValidSQL’, ‘P’) IS NOT NULL
DROP PROCEDURE dbo.IsValidSQL;
GO
CREATE PROC [dbo].[IsValidSQL]
( @sql VARCHAR(MAX) )
AS
BEGIN
  BEGIN TRY
    SET @sql = 'set parseonly on;' + @sql;
    EXEC (@sql);
  END TRY
  BEGIN CATCH
    RETURN(1);
  END CATCH;
  RETURN(0);
END;
Advertisements