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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s