A U-Bend In The Tubes

Friday, July 24, 2009

M$-SQL Server GO Keyword is a badly thought out piece of shite.

Just a quick one. The database for my company’s main software product uses M$-SQL, since every two bit company in the universe has a license for it kicking about somewhere, and while adding my changes to the migration script I discovered that the ‘GO’ keyword makes declared variables go out of scope, thus:

DECLARE @ID uniqueidentifier

SELECT @id = FOO.id
    FROM DataEntityExtractors FOO
    WHERE FOO.id = 'id'
GO

PRINT @ID
---------------

Error: Must declare the scalar variable "@ID".
SQLState:  S1000
ErrorCode: 137
Error occured in:
PRINT @ID

To make it work you need to move the ‘GO’ to a point in the script where you don’t need the variables anymore:

DECLARE @ID uniqueidentifier

SELECT @id = FOO.id
    FROM DataEntityExtractors FOO
    WHERE FOO.id = 'id'

PRINT @ID
GO

Is a version of the first query that will work.

Goddamn I hate M$-SQL. Hate it so very, very much. No idea why you need to second guess the query compiler before you can get stuff to work.

Meh.

posted by Bradshaw at 15:24  

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

Powered by WordPress