Alter table fails

  • I have made this little script:

    SELECT is_Nullable FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '3NF_Medarbejder' AND COLUMN_NAME = 'Fornavn'

    ALTER TABLE 3NF_Medarbejder

    ALTER COLUMN fornavn NULL

    SELECT is_Nullable FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '3NF_Medarbejder' AND COLUMN_NAME = 'Fornavn'

    First line gives me the status of coloumn 'Fornavn', and executes correctly

    First line gives the answer 'No'

    Next two lines is the problem - It tells me that i have a syntax error near 3

    I really do not understand - As line one is referring to the very same table and coloumn.

    What Am i doing wrong?

    Best regards

    Edvard Korsbæk

  • You need to separate out each call using the batch terminator GO. So you need:

    SELECT ... FROM ...

    GO

    ALTER TABLE ...

    GO

    SELECT ... FROM ...

    GO

    Jeffrey Williams
    “We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”

    ― Charles R. Swindoll

    How to post questions to get better answers faster
    Managing Transaction Logs

  • If I add 'Go'

    SELECT is_Nullable FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '3NF_Medarbejder' AND COLUMN_NAME = 'Fornavn'

    GO

    ALTER TABLE 3NF_Medarbejder

    ALTER COLUMN fornavn NULL

    GO

    SELECT is_Nullable FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '3NF_Medarbejder' AND COLUMN_NAME = 'Fornavn'

    Line 1 and line 6 executes, but line s fails with:

    Msg 102, Level 15, State 1, Line 1

    Incorrect syntax near '3'.

    i.e. its the Alter Table that fails, but I still cannot figure out why

    Edvard Korsbæk

  • Because a table name cannot start with a number, unless you quote it. You can quote it using either brackets or double-quotes. For example:

    ALTER TABLE [3NF_Medarbejder] ALTER COLUMN fornavn datatype NULL;

    You also need to specify the data type for the column.

    Jeffrey Williams
    “We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”

    ― Charles R. Swindoll

    How to post questions to get better answers faster
    Managing Transaction Logs

  • Thanks!

    Did the trick

    A newbie is a newbie untill an expert has converted:-)

    Edvard Korsbæk

Viewing 5 posts - 1 through 5 (of 5 total)

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