Mazeryt
Mazeryt

Reputation: 915

Conditional SQL statement incorrect syntax

I have problem with conditional SQL script in SQL Server 2008.

When I run each command as separate everything is OK and I successfully update my DB

But when I split everything into one command I get errors:

Msg 102, Level 15, State 1, Line 3
Incorrect syntax near ';'.

Msg 207, Level 16, State 1, Line 1
Invalid column name 'sAdvertTypeCode'.

Msg 102, Level 15, State 1, Line 2
Incorrect syntax near 'end'.

So I tried without semicolon ';'

And error was:

Msg 102, Level 15, State 1, Line 3
Incorrect syntax near ')'.

Msg 207, Level 16, State 1, Line 1
Invalid column name 'sAdvertTypeCode'.

Msg 102, Level 15, State 1, Line 2
Incorrect syntax near 'end'.

My script :

IF COL_LENGTH('TechnicalDialogue', 'sAdvertTypeCode') IS NULL
begin
    ALTER TABLE TechnicalDialogue ADD sAdvertTypeCode varchar(40) ;
    go
    UPDATE TechnicalDialogue SET sAdvertTypeCode = 'advert.type.broadcast' ;
    go
    Select * FROM TechnicalDialogue;
end

I don't have a lot experience with SQL Server so I'm a little bit confused.

Upvotes: 0

Views: 380

Answers (1)

Matt Gibson
Matt Gibson

Reputation: 38238

GO isn't a SQL statement. It's a command used in the client to execute batches of SQL, so you can't have it in the middle of a BEGIN/END. GO will send everything since the start of the script or the last GO to the server for execution, so the server sees a begin but no end. – Matt Gibson 26 mins ago

Upvotes: 3

Related Questions