SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


EXECUTE


EXECUTE

Author
Message
Hugo Kornelis
Hugo Kornelis
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10789 Visits: 11966
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
cengland0
cengland0
SSCommitted
SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)

Group: General Forum Members
Points: 1664 Visits: 1300
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 :-)
Hugo Kornelis
Hugo Kornelis
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10789 Visits: 11966
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)
:-D


Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
cengland0
cengland0
SSCommitted
SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)

Group: General Forum Members
Points: 1664 Visits: 1300
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)
:-D

Gee thanks. You had to ruin my fun didn't you? ;-)
rfr.ferrari
rfr.ferrari
SSCommitted
SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)

Group: General Forum Members
Points: 1529 Visits: 13627
good question!!!
thanks!


rfr.ferrari
DBA - SQL Server 2008
MCITP | MCTS

remember is live or suffer twice!
the period you fastest growing is the most difficult period of your life!

Koen Verbeeck
Koen Verbeeck
One Orange Chip
One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)

Group: General Forum Members
Points: 27051 Visits: 13268
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?
My blog at SQLKover.

MCSE Business Intelligence - Microsoft Data Platform MVP
TomThomson
TomThomson
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14160 Visits: 12197
Nice question, but the answer is a complete fail Angry.
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 Hehe.

And I'm appalled at all those earlier replies that indicate no-one noticed this simple coding error.

Tom

John Mitchell-245523
John Mitchell-245523
SSChampion
SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)

Group: General Forum Members
Points: 13882 Visits: 15926
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
skanker
skanker
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1139 Visits: 1385
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.:-D
bminch
bminch
SSC Rookie
SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)

Group: General Forum Members
Points: 46 Visits: 53
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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search