What is wrong with this syntak ? Why am I getting this error

  • RE: The following code gives the error message :

    Msg 156, Level 15, State 1, Line 6

    Incorrect syntax near the keyword 'SCHEMA'.

    -----------------------------------------------------------------

    IF not exists ( Select * FROM SYS.SCHEMAS where name = 'CLAIM' )

    Begin

    CREATE SCHEMA CLAIM

    PRINT 'Schema Created';

    End

  • CREATE SCHEMA must be the only statement in the batch.

    Weird enough, this is considered a single statement.

    CREATE SCHEMA CLAIM

    CREATE TABLE Claims( ClaimId int)

    CREATE TABLE OtherTable( OtherId int)

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • mw112009 (8/10/2016)


    RE: The following code gives the error message :

    Msg 156, Level 15, State 1, Line 6

    Incorrect syntax near the keyword 'SCHEMA'.

    -----------------------------------------------------------------

    IF not exists ( Select * FROM SYS.SCHEMAS where name = 'CLAIM' )

    Begin

    CREATE SCHEMA CLAIM

    PRINT 'Schema Created';

    End

    It doesn't like the IF. Try this:

    IF not exists ( Select * FROM SYS.SCHEMAS where name = 'CLAIM' )

    Begin

    exec ('CREATE SCHEMA CLAIM');

    PRINT 'Schema Created';

    End

    ELSE

    PRINT 'Schema already Exists';

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply