0

I have few scripts which either creates new objects like SP or view/functions and some which just ALTERS them.

Now how should I create one standard script where my script can work for above work

Example: using CREATE OR ALTER SCRIPT does not work for versions < 2016

How should I write scripts that works from SQL2012 to SQL2019 versions

Dale K
  • 25,246
  • 15
  • 42
  • 71
Newbie-DBA
  • 107
  • 5

1 Answers1

0

Something like this should work.

DECLARE @Command     VARCHAR(MAX)
        ,@ObjectSchemaName VARCHAR(MAX) = 'dbo.SomeTable'
        ,@ObjectType VARCHAR(50)  = 'TABLE';

IF EXISTS (
              SELECT    1
              FROM      sys.objects AS o
                        INNER JOIN sys.schemas AS s ON s.schema_id = o.schema_id
              WHERE     s.name + '.' + o.name = @ObjectSchemaName
          )
BEGIN
    SET @Command = 'ALTER ' + @ObjectType + ' AS SOME CODE HERE ';
END;
ELSE BEGIN
SET @Command = 'CREATE ' + @ObjectType + ' AS SOME CODE HERE ';
END;

SELECT @Command
Mike Petri
  • 570
  • 3
  • 10