Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12345»»»

EXECUTE Expand / Collapse
Author
Message
Posted Tuesday, February 14, 2012 2:22 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 5:19 PM
Points: 6,000, Visits: 8,263
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
Post #1251724
Posted Tuesday, February 14, 2012 2:49 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen 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
Post #1251738
Posted Tuesday, February 14, 2012 2:55 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 5:19 PM
Points: 6,000, Visits: 8,263
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
Post #1251746
Posted Tuesday, February 14, 2012 3:01 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen 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?
Post #1251752
Posted Tuesday, February 14, 2012 3:04 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Saturday, August 30, 2014 9:14 AM
Points: 1,258, Visits: 13,554
good question!!!
thanks!



rfr.ferrari
DBA - SQL Server 2008
MCITP | MCTS

remember is live or suffer twice!
Post #1251757
Posted Tuesday, February 14, 2012 3:21 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 6:15 AM
Points: 13,730, Visits: 10,684
Hugo Kornelis (2/14/2012)
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



Ah, that is good to know.




How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?

Member of LinkedIn. My blog at LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1251767
Posted Tuesday, February 14, 2012 4:09 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Yesterday @ 6:30 PM
Points: 8,830, Visits: 9,388
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
Post #1251805
Posted Tuesday, February 14, 2012 4:30 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 5:54 AM
Points: 5,369, Visits: 9,894
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
Post #1251807
Posted Tuesday, February 14, 2012 6:26 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Today @ 6:15 AM
Points: 900, Visits: 1,247
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.
Post #1251871
Posted Tuesday, February 14, 2012 6:30 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, July 31, 2014 9:56 AM
Points: 37, Visits: 38
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

Post #1251874
« Prev Topic | Next Topic »

Add to briefcase ««12345»»»

Permissions Expand / Collapse