April 21, 2010 at 11:27 am
Oleg Netchaev (4/21/2010)
Since that time I got into habit of always starting the cte expression with a semicolon
I never understand that habit. The semicolon is a statement terminator, not a statement starter, so the logical place for it is at the end of a statement, not before the next one.
The semicolon has always been part of the Transact SQL syntax, but unlike almost every other language, it was optional. And hence, nobody ever used it. This first changed when the SQL Server 2005 parser required a statement to be terminated in order to recognise WITH as the CTE starter, not as a query hint. But in SQL Server 2008, the SQL team went a step further and added "Not using a statement terminator for Transact-SQL statements" to the list of deprecated features. This means that in some future version, you will be required to terminate all statements.
So do as I do - start to accustom yourself to terminating all statements with a semicolon today. You'll be thankful later. (And you'll never have to worry about the semicolon before a CTE anymore).
April 21, 2010 at 11:38 am
Hugo Kornelis (4/21/2010)
Oleg Netchaev (4/21/2010)
Since that time I got into habit of always starting the cte expression with a semicolonI never understand that habit. The semicolon is a statement terminator, not a statement starter, so the logical place for it is at the end of a statement, not before the next one.
The semicolon has always been part of the Transact SQL syntax, but unlike almost every other language, it was optional. And hence, nobody ever used it. This first changed when the SQL Server 2005 parser required a statement to be terminated in order to recognize WITH as the CTE starter, not as a query hint. But in SQL Server 2008, the SQL team went a step further and added "Not using a statement terminator for Transact-SQL statements" to the list of deprecated features. This means that in some future version, you will be required to terminate all statements.
So do as I do - start to accustom yourself to terminating all statements with a semicolon today. You'll be thankful later. (And you'll never have to worry about the semicolon before a CTE anymore).
Thank you Hugo,
I have been using the semicolons in every statement for at least 2 years now and I try not to fail on this one. I can see that the guideline about placing one before the cte makes sense though. This recommendation is caused precisely by the fact that majority of people still don't use semicolons, so placing one before cte definition is simply to assure that no errors are introduced by someone forgetting to terminate their statement with semicolon. Typical example would be when different developers submit new scripts, one of such scripts begins with cte, and if the previous one was written by someone who did not terminate his last statement then the error will be introduced into the final script should all the small ones be combined.
Oleg
April 21, 2010 at 11:46 am
Oleg Netchaev (4/21/2010)
Hugo Kornelis (4/21/2010)
Oleg Netchaev (4/21/2010)
Since that time I got into habit of always starting the cte expression with a semicolonI never understand that habit. The semicolon is a statement terminator, not a statement starter, so the logical place for it is at the end of a statement, not before the next one.
The semicolon has always been part of the Transact SQL syntax, but unlike almost every other language, it was optional. And hence, nobody ever used it. This first changed when the SQL Server 2005 parser required a statement to be terminated in order to recognize WITH as the CTE starter, not as a query hint. But in SQL Server 2008, the SQL team went a step further and added "Not using a statement terminator for Transact-SQL statements" to the list of deprecated features. This means that in some future version, you will be required to terminate all statements.
So do as I do - start to accustom yourself to terminating all statements with a semicolon today. You'll be thankful later. (And you'll never have to worry about the semicolon before a CTE anymore).
Thank you Hugo,
I have been using the semicolons in every statement for at least 2 years now and I try not to fail on this one. I can see that the guideline about placing one before the cte makes sense though. This recommendation is caused precisely by the fact that majority of people still don't use semicolons, so placing one before cte definition is simply to assure that no errors are introduced by someone forgetting to terminate their statement with semicolon. Typical example would be when different developers submit new scripts, one of such scripts begins with cte, and if the previous one was written by someone who did not terminate his last statement then the error will be introduced into the final script should all the small ones be combined.
Oleg
I still disagree. This is used as a catch-all to avoid teaching moments. I don't use the ; at the beginning of the cte, but rather as a terminator at the previous clause. Not all cte's require a ;, since many do not follow another batch or statement. If somebody doesn't terminate their statement properly, then teach them and catch the error via code review.
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
April 21, 2010 at 11:59 am
CirquedeSQLeil (4/21/2010)[hr
I still disagree. This is used as a catch-all to avoid teaching moments. I don't use the ; at the beginning of the cte, but rather as a terminator at the previous clause. Not all cte's require a ;, since many do not follow another batch or statement. If somebody doesn't terminate their statement properly, then teach them and catch the error via code review.
I would really, really love it if I could come to agreement with every developer in our team on the semicolon usage. Unfortunately, the usual response I get is something like "the semicolons are not required yet, so why bother using them". I totally disagree with this logic, but it is prevalent for now.
All I was trying to point out was that from the perspective of the person who actually wrote the recommendation it does make sense considering the facts that unfortunately, many people still don't use semicolons 🙁
Oleg
April 21, 2010 at 12:25 pm
Oleg Netchaev (4/21/2010)
CirquedeSQLeil (4/21/2010)[hr
I still disagree. This is used as a catch-all to avoid teaching moments. I don't use the ; at the beginning of the cte, but rather as a terminator at the previous clause. Not all cte's require a ;, since many do not follow another batch or statement. If somebody doesn't terminate their statement properly, then teach them and catch the error via code review.
I would really, really love it if I could come to agreement with every developer in our team on the semicolon usage. Unfortunately, the usual response I get is something like "the semicolons are not required yet, so why bother using them". I totally disagree with this logic, but it is prevalent for now.
All I was trying to point out was that from the perspective of the person who actually wrote the recommendation it does make sense considering the facts that unfortunately, many people still don't use semicolons 🙁
Oleg
Point taken. Make sure you give each of them 15 lashes for disobedience too.
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
April 21, 2010 at 4:25 pm
Hugo Kornelis (4/21/2010)
I never understand that habit. The semicolon is a statement terminator, not a statement starter, so the logical place for it is at the end of a statement, not before the next one.
This is one of those (perhaps irrational) things that bothers me too - it makes my brain jolt every time I see that ;WITH construction used. On the other hand, I dislike column lists prefixed with a comma (though I do see why people do that) as well, so maybe it is just me.
Paul White
SQLPerformance.com
SQLkiwi blog
@SQL_Kiwi
April 21, 2010 at 4:43 pm
Paul White NZ (4/21/2010)
Hugo Kornelis (4/21/2010)
I never understand that habit. The semicolon is a statement terminator, not a statement starter, so the logical place for it is at the end of a statement, not before the next one.This is one of those (perhaps irrational) things that bothers me too - it makes my brain jolt every time I see that ;WITH construction used. On the other hand, I dislike column lists prefixed with a comma (though I do see why people do that) as well, so maybe it is just me.
I'm on the fence with the comma prefix. I definately have been using the ; at the end of the code block althought It still isn't a habbit and sometimes I forget to do it.; (lol)
---------------------------------------------------------------------
Use Full Links:
KB Article from Microsoft on how to ask a question on a Forum
April 21, 2010 at 4:58 pm
Paul White NZ (4/21/2010)
Hugo Kornelis (4/21/2010)
I never understand that habit. The semicolon is a statement terminator, not a statement starter, so the logical place for it is at the end of a statement, not before the next one.This is one of those (perhaps irrational) things that bothers me too - it makes my brain jolt every time I see that ;WITH construction used. On the other hand, I dislike column lists prefixed with a comma (though I do see why people do that) as well, so maybe it is just me.
I'm one who likes to have the comma in front of the column in a list. I do it for ease of commenting out a line if I decide that column should not be included without having to comment out a comma somewhere else. Though - if people were consistent with their commas it wouldn't matter too much and things would line up more neatly.
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
April 23, 2010 at 10:10 am
The question is misleadingly worded. A view definition can contain a CTE, but the CTE is not "part of" the SELECT statement. The CTE can contain its own SELECT statement, and can precede the defining SELECT statement of the view.
May 3, 2010 at 5:27 am
learnt something new.
"Keep Trying"
July 27, 2010 at 9:01 pm
Hugo Kornelis (4/21/2010)
I never understand that habit. The semicolon is a statement terminator, not a statement starter, so the logical place for it is at the end of a statement, not before the next one.The semicolon has always been part of the Transact SQL syntax, but unlike almost every other language, it was optional.
Can you suggest a language in which it is used as a terminator rather than as a separator? I can't think of one.
I remember the early C++ compilers (the ones which compiled C++ into C and then called a C compiler to get a binary) which were sensitive to the fact that it was not a terminator, but a separator, and didn't like empty statements. So if you wrote a semicolon at the end of the last statement before the terminating brace of a compound statement the parser threw an error. You also got an error if you wrote a semicolon after the closing brace of a compound statement.
Is T-SQL going to break with this tradition and allow semicolon immediately before or immediately after END?
And hence, nobody ever used it. This first changed when the SQL Server 2005 parser required a statement to be terminated in order to recognise WITH as the CTE starter, not as a query hint.
And that was a bad piece of language design. If the parser team were unable conveniently to distinguish the two uses of "WITH" (which suggests either incompetence or unreasonable schedule presure) they should have insisted on a different keyword being used for CTEs, to preserve the language's ability to live without a statement separator.
But in SQL Server 2008, the SQL team went a step further and added "Not using a statement terminator for Transact-SQL statements" to the list of deprecated features. This means that in some future version, you will be required to terminate all statements.
I can't think of a polite way to express my opinion of this MS decision.
So do as I do - start to accustom yourself to terminating all statements with a semicolon today. You'll be thankful later. (And you'll never have to worry about the semicolon before a CTE anymore).
I probably won't do that. I'm old enough that I could happily stop using T-SQL when that change comes in. I'm already getting hell from my wife because I'm trying to work for 8 to 16 weeks a year instead of retiring completely, so doing a couple of weeks on non-database stuff instead of a few months on database when this nonsense kicks in a a few years time might benefit me more from the reduction in nagging I get than it costs me from the loss of fun things to do, so I could stick instead to my beloved declarative (functional, logical, or mathematical) languages which don't need statement separators because each has a decent syntax. And finding 4 weeks work in one of those areas will probably be about as easy as finding 16 weeks as a data management expert.
Tom
August 1, 2010 at 3:32 pm
Tom.Thomson (7/27/2010)
Hugo Kornelis (4/21/2010)
I never understand that habit. The semicolon is a statement terminator, not a statement starter, so the logical place for it is at the end of a statement, not before the next one.The semicolon has always been part of the Transact SQL syntax, but unlike almost every other language, it was optional.
Can you suggest a language in which it is used as a terminator rather than as a separator? I can't think of one.
I remember the early C++ compilers (the ones which compiled C++ into C and then called a C compiler to get a binary) which were sensitive to the fact that it was not a terminator, but a separator, and didn't like empty statements. So if you wrote a semicolon at the end of the last statement before the terminating brace of a compound statement the parser threw an error. You also got an error if you wrote a semicolon after the closing brace of a compound statement.
Is T-SQL going to break with this tradition and allow semicolon immediately before or immediately after END?
Yes. The semicolon is a terminator in T-SQL, not a seperator. This is valid code:
IF @Var = 1
BEGIN;
PRINT 'It is 1';
END;
ELSE
BEGIN;
PRINT 'It is not 1';
END;
There is no semicolon after IF @Var = 1 or after ELSE because they need a statement (or a BEGIN END block) to be complete.
There is one annoying exception - in a TRY CATCH block, you get an error when you terminate END TRY with a semicolon. :crying:
I don't know enough about C++ to comment about the terminator / seperator difference, so I'll just take your word for it.
And hence, nobody ever used it. This first changed when the SQL Server 2005 parser required a statement to be terminated in order to recognise WITH as the CTE starter, not as a query hint.
And that was a bad piece of language design. If the parser team were unable conveniently to distinguish the two uses of "WITH" (which suggests either incompetence or unreasonable schedule presure) they should have insisted on a different keyword being used for CTEs, to preserve the language's ability to live without a statement separator.
Only they could not use anything but WITH for the CTE start, because that is how a CTE is defined in the ANSI standard for SQL. And they could not change the use of WITH for hints either, cause that would break existing code.
November 3, 2012 at 3:16 am
Nice and clean question..
Good discussion 😛
_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
January 31, 2013 at 5:53 am
nice question.... Easy understandable about both view and CTE....
Manik
**********
Manik
You cannot get to the top by sitting on your bottom.
Viewing 14 posts - 16 through 28 (of 28 total)
You must be logged in to reply to this topic. Login to reply