February 13, 2012 at 8:59 pm
Comments posted to this topic are about the item EXECUTE
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
This thing is addressing problems that dont exist. Its solution-ism at its worst. We are dumbing down machines that are inherently superior. - Gilfoyle
February 13, 2012 at 9:00 pm
February 13, 2012 at 9:50 pm
Nice question. Thanks
Thanks
February 14, 2012 at 12:13 am
Great question, thanks Henrico.
I stumbled across this issue a few times too many, so a real easy one for me 🙂
Need an answer? No, you need a question
My blog at https://sqlkover.com.
MCSE Business Intelligence - Microsoft Data Platform MVP
February 14, 2012 at 12:41 am
Koen Verbeeck (2/14/2012)
Great question, thanks Henrico.I stumbled across this issue a few times too many, so a real easy one for me 🙂
You're not alone, Koen 😛
February 14, 2012 at 1:14 am
Good question - i got it right.
But surely without running the code there would be no output :0)
(Above is tongue in cheek)
February 14, 2012 at 1:16 am
danielfountain (2/14/2012)
But surely without running the code there would be no output :0)
Indeed! :w00t:
I'm missing the answer "after waiting for a few hours you eventually decide to go home..."
Need an answer? No, you need a question
My blog at https://sqlkover.com.
MCSE Business Intelligence - Microsoft Data Platform MVP
February 14, 2012 at 1:46 am
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.
February 14, 2012 at 2:05 am
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.
Need an answer? No, you need a question
My blog at https://sqlkover.com.
MCSE Business Intelligence - Microsoft Data Platform MVP
February 14, 2012 at 2:13 am
Koen Verbeeck (2/14/2012)
kll 51891 (2/14/2012)
I would have expected it to fail due to no newline or semicolon.<snip>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.
Thank you for the clarification.
A quick test:
use master
select top 10 * from sys.all_columns
select top 10 * from sys.all_objects
select top 10 * from sys.all_columns select top 10 * from sys.all_objects
select top 10 * from sys.all_columns ;; select top 10 * from sys.all_objects
reveals that you are correct in the missing semicolons are OK. But it doesn't show why an abundance of semicolons should have detrimental effects.
My quoting of ";with" were somewhat tongue-in-cheek as it is the first keyword that I am aware of that doesn't accept a newline as a break in command.
Anyway, looking objectively at stuff, then the fact that I can write
Select *
From xx
Where something is right
should warn me that semicolons were problably superflous.
February 14, 2012 at 2:22 am
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
February 14, 2012 at 2:49 am
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 🙂
February 14, 2012 at 2:55 am
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)
😀
February 14, 2012 at 3:01 am
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? 😉
February 14, 2012 at 3:04 am
good question!!!
thanks!
Viewing 15 posts - 1 through 15 (of 49 total)
You must be logged in to reply to this topic. Login to reply