August 18, 2014 at 11:46 pm
Comments posted to this topic are about the item declarations
Tom
August 19, 2014 at 12:33 am
Easy, thx 🙂
Thanks & Best Regards,
Hany Helmy
SQL Server Database Consultant
August 19, 2014 at 12:33 am
Easy one if you remember Toms explanation of the previous QotD:
DECLARE @i INT = 0;
equals
DECLARE @i INT;
SET @i = 0;
Thanks for the question!
Need an answer? No, you need a question
My blog at https://sqlkover.com.
MCSE Business Intelligence - Microsoft Data Platform MVP
August 19, 2014 at 2:32 am
This was removed by the editor as SPAM
August 19, 2014 at 4:08 am
Koen Verbeeck (8/19/2014)
Easy one if you remember Toms explanation of the previous QotD:DECLARE @i INT = 0;
equals
DECLARE @i INT;
SET @i = 0;
Thanks for the question!
Agreed.
August 19, 2014 at 5:19 am
andy.brown (8/19/2014)
Koen Verbeeck (8/19/2014)
Easy one if you remember Toms explanation of the previous QotD:DECLARE @i INT = 0;
equals
DECLARE @i INT;
SET @i = 0;
Thanks for the question!
Agreed.
Thanks for the question, Tom. I genuinely appreciate the syntax that allows declaration and initial assignment, but this is proof that they're really separate. I hope I don't see code like this in production.
August 19, 2014 at 5:30 am
Good question, Tom. Thanks!
This is a good example of how things can go wrong when using this syntax.
---------------
Mel. 😎
August 19, 2014 at 8:08 am
Nice question, Tom. Thanks.
I like this syntax, if it is used properly (and not in the way documented here). I work in SQL Server and in Sybase ASE, which does not have this syntax, which makes me appreciate it even more. It's a shame that BOL doesn't document this very well (unlike the rest of BOL, which is, of course, VERY complete;-)).
August 19, 2014 at 8:08 am
Unless you're running 2005 or older, in which case you'd have a nice little syntax error happening 😉
August 19, 2014 at 10:25 am
Thank you, Tom, very interesting one.
As the old saying goes "everything has pros and cons", so any one see the "pros" side of it?
(I am kind of feeling now, its one of the stupid question from me)
ww; Raghu
--
The first and the hardest SQL statement I have wrote- "select * from customers" - and I was happy and felt smart.
August 19, 2014 at 12:17 pm
Raghavendra Mudugal (8/19/2014)
Thank you, Tom, very interesting one.As the old saying goes "everything has pros and cons", so any one see the "pros" side of it?
(I am kind of feeling now, its one of the stupid question from me)
The pro is of course that if you put all the declarations combined with assignment in a non-repeated part of the stored procedure, trigger, or batch the assignments get executed exactly once, so the net effect is much the same as declaration with initialisation to a chosen value and this can quite useful for making the code easier to read (it reduces clutter).
Something that is often claimed as a plus for this is that it eliminates the possibility of uninitialised variables, but that's pure nonsense. It doesn't have the effect that adding initialisation to declarations in some other languages, specifically languages which have variable declarations without initialisation, since (scalar) variable declarations in T-SQL initialise the variable to NULL anyway: there no such thing as an uninitialised variable in T-SQL; thus the claim that it eliminates the uninitialised variable problem from T-SQL (which I've seen rather too often) is nonsense, don't be fooled into thinking that that imaginary effect is a plus of allowing combined declaration and assignment syntax as an abbreviation for a declaration plus a set statement.
edit: typos
Tom
August 19, 2014 at 12:57 pm
Nice and easy one. Thanks Tom.
August 19, 2014 at 1:41 pm
TomThomson (8/19/2014)
The pro is of course that if you put all the declarations combined with assignment in a non-repeated part of the stored procedure, trigger, or batch the assignments get executed exactly once, so the net effect is much the same as declaration with initialisation to a chosen value and this can quite useful for making the code easier to read (it reduces clutter).
Thank you, Tom. When I came to know the in-line assignment types, I said, great as I did in VB.NET, now I can do in SQL too, probably, in future versions there will also be an option to mention ByVal or ByRef (like stuff) in procedure/function level....and slowing sql scripting will be taken over by OOPS concepts.
ww; Raghu
--
The first and the hardest SQL statement I have wrote- "select * from customers" - and I was happy and felt smart.
Viewing 15 posts - 1 through 15 (of 18 total)
You must be logged in to reply to this topic. Login to reply