|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 2:48 AM
Points: 5,294,
Visits: 7,232
|
|
Koen Verbeeck (2/14/2012)
kll 51891 (2/14/2012) I would have expected it to fail due to no newline or semicolon. But it were not so Anywhere I can find specific rules about when to use and when not to use semicolon? Except the reserved word ";with" of course.
The semicolon is not yet obligated, except when using the WITH clause. This probably will change in a future version. If there's a statement before the WITH clause, it should be terminated with a semicolon. Pay attention, this is not the same as saying that it should be ";WITH". If everyone starts terminating statements with semicolon in old code to make the code portable to a newer edition of SQL Server, all ;WITH statements will fail.
Most of the above is true.
Terminiating SQL statements with a semicolon has always been allowed in SQL Server, but it was optional. WITH was the first keyword (but is not the only one) that requires the statement before it (if any) to be semicolon-terminated, otherwise the parser would think that the WITH keyword was for query hints. It has already been announced that in some future version, omitting the semicolon will be disallowed in all cases. There has not been any mention as to which version that will be. But it's a good idea to start getting into the habit now of always using the semicolon statement terminator.
I consider the use of ;WITH a bad habit, as it works around one limitation while not addressing the true underlying issue. And it will come back to bite you when terminating is no longer optional. However, it does currently work - not because he parser accepts prepending WITH with a semicolon as a viable alternative, but because the parser ignores whitespace and line breaks - so it "sees" the semicolon right after the end of the preceding statement and interprets it as a statement terminator.
However, it is not true that ;WITH will cause errors when all statements are semicolon terminated. There is no limit to how many semicolons you use and where you place them. The code below, though clearly not recommended coding style, works.
SELECT 1;;; ;;; ;;;WITH x AS (SELECT 1 AS a) SELECT * FROM x
Hugo Kornelis, SQL Server MVP Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Thursday, January 24, 2013 9:59 PM
Points: 1,354,
Visits: 1,299
|
|
Thanks for the great question. Couple points:
1. I thought both statements were exactly the same. I sat there looking at it for about 4 minutes before I noticed the difference between the two. I must be going blind.
2. Technically, both statements could execute if you just happened to have a stored procedure with the name [use master dbcc showfilestats]. I know, this is crazy but did want to be the first to mention it
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 2:48 AM
Points: 5,294,
Visits: 7,232
|
|
cengland0 (2/14/2012)
Technically, both statements could execute if you just happened to have a stored procedure with the name [use master dbcc showfilestats]. I know, this is crazy but did want to be the first to mention it  I'm sorry, but you are too late. Henrico already covered that crazy possibility in the explanation of the question: "SQL will assume '@sqlstring' is a Stored Procedure and will fail, assuming it doesn't exist." (emphasis mine)
Hugo Kornelis, SQL Server MVP Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Thursday, January 24, 2013 9:59 PM
Points: 1,354,
Visits: 1,299
|
|
Hugo Kornelis (2/14/2012)
cengland0 (2/14/2012)
Technically, both statements could execute if you just happened to have a stored procedure with the name [use master dbcc showfilestats]. I know, this is crazy but did want to be the first to mention it  I'm sorry, but you are too late. Henrico already covered that crazy possibility in the explanation of the question: "SQL will assume '@sqlstring' is a Stored Procedure and will fail, assuming it doesn't exist." (emphasis mine)  Gee thanks. You had to ruin my fun didn't you?
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Thursday, June 06, 2013 4:06 PM
Points: 1,219,
Visits: 13,509
|
|
good question!!! thanks!
rfr.ferrari DBA - SQL Server 2008 MCITP | MCTS
remember is live or suffer twice!
|
|
|
|
|
SSCrazy Eights
        
Group: General Forum Members
Last Login: Yesterday @ 6:30 AM
Points: 9,410,
Visits: 6,495
|
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Yesterday @ 4:53 PM
Points: 7,182,
Visits: 7,281
|
|
Nice question, but the answer is a complete fail . The whole batch will in fact fail because a variable can only be declared once ion a batch, so the batch is failed and neither statement is executed. The nearest option to that in the answers is "both fail".
Had the question been "which batch will fail" the given answer would have been correct, but the question was about statements and the code given was a single batch with the same variable declared twice.
Looks like a lack of quality assurance to me .
And I'm appalled at all those earlier replies that indicate no-one noticed this simple coding error.
Tom Is minic a gheibheann béal oscailte dorn dúnta. Is minig a cheapas beul fosgailte dòrn dùinte.
http://es.linkedin.com/in/tomthomsonsoftware
|
|
|
|
|
SSCarpal Tunnel
       
Group: General Forum Members
Last Login: Tuesday, June 04, 2013 7:03 AM
Points: 4,443,
Visits: 7,249
|
|
Appalled? I usually reserve that reaction for earthquakes, famines, wars and so on.
In any case, I don't agree that just because two statements are posted on the same page without a GO between them, we should necessarily assume that they are to be executed as a single batch. OK, so the question could have said "assuming that they are executed separately", but if we start down that road, we'd end up with a list of terms and conditions longer than the question itself. In my opinion, the intent of this question was clear.
John
|
|
|
|
|
Say Hey Kid
      
Group: General Forum Members
Last Login: Yesterday @ 8:40 AM
Points: 703,
Visits: 1,032
|
|
Good question. Got it wrong and thanks to the explanations offered I have learnt something today, which is always a good thing as I have lots to learn.
Thanks to you all.
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Tuesday, May 21, 2013 7:06 AM
Points: 32,
Visits: 29
|
|
While its certainly easier to use brackets/parenthesis around @sqlstring don't forget about sp_executesql.
declare @sqlstring nvarchar (255) set @sqlstring = 'use ' + 'master' + ' dbcc showfilestats' execute sp_executesql @sqlstring
|
|
|
|