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 11:05 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 9:02 AM
Points: 1,383, Visits: 1,754
Hugo Kornelis (2/14/2012)
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.


I also consider ;WITH a bad habit because it already breaks things. If you use WITH a lot, and get in the habit of prepending the semicolon, chances are you'll end up writing code like the following:
CREATE VIEW v_sample AS
;WITH a AS ...

which will not execute because the semicolon terminates the CREATE VIEW statement before it's complete. Sure, that's easy to fix, but if you're in the habit of prepending the semicolon, you'll have to fix it every time. If, on the other hand, you get in the habit of terminating statements with the semicolon instead, you won't have anything to fix.

Post #1252059
Posted Tuesday, February 14, 2012 11:10 AM


UDP Broadcaster

UDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP Broadcaster

Group: General Forum Members
Last Login: 2 days ago @ 8:20 AM
Points: 1,490, Visits: 1,060
John Mitchell-245523 (2/14/2012)
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


+1
Post #1252065
Posted Tuesday, February 14, 2012 2:24 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Thursday, December 18, 2014 10:56 AM
Points: 3,417, Visits: 2,035
John Mitchell-245523 (2/14/2012)
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


+1

Also, the Merge statement is another one that requires a semicolon at the end.
Post #1252158
Posted Tuesday, February 14, 2012 3:30 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, January 31, 2013 8:53 AM
Points: 1,176, Visits: 778
tks for the question
Post #1252186
Posted Tuesday, February 14, 2012 9:22 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Thursday, August 28, 2014 2:39 AM
Points: 1,880, Visits: 2,846
Obligatory pedantic complaint: they're parentheses, not brackets. These are brackets: []. (For completeness, these are braces: {}.)


Noted; thank you.


----------------------------------------------
Msg 8134, Level 16, State 1, Line 1
Divide by zero error encountered.
Post #1252234
Posted Wednesday, February 15, 2012 2:16 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Thursday, December 18, 2014 10:22 AM
Points: 1,830, Visits: 6,646
sknox (2/14/2012)
Obligatory pedantic complaint: they're parentheses, not brackets. These are brackets: []. (For completeness, these are braces: {}.)


Where I come from, () are brackets, [] are square brackets, and {} are curly brackets
Post #1252314
Posted Wednesday, February 15, 2012 5:10 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: 2 days ago @ 4:18 PM
Points: 9,928, Visits: 11,206
Good question.



Paul White
SQL Server MVP
SQLblog.com
@SQL_Kiwi
Post #1252418
Posted Wednesday, February 15, 2012 5:15 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: 2 days ago @ 4:18 PM
Points: 9,928, Visits: 11,206
Koen Verbeeck (2/14/2012)
The semicolon is not yet obligated, except when using the WITH clause.

MERGE (the statement, not the join hint!) is required to be terminated with a semicolon.




Paul White
SQL Server MVP
SQLblog.com
@SQL_Kiwi
Post #1252421
Posted Wednesday, February 15, 2012 3:11 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, February 17, 2014 9:44 AM
Points: 127, Visits: 105
Good Question... Thank you...
Post #1252775
Posted Wednesday, February 15, 2012 8:53 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, December 2, 2014 6:21 AM
Points: 169, Visits: 286
One question though if you execute you should get error below,

Msg 134, Level 15, State 1, Line 9
The variable name '@sqlstring' has already been declared. Variable names must be unique within a query batch or stored procedure.

once you fix the above may be your answer would right :)


Cheer Satish
Post #1252842
« Prev Topic | Next Topic »

Add to briefcase «««12345»»

Permissions Expand / Collapse