| Author |
Topic |
|
jezemine
Master Smack Fu Yak Hacker
2886 Posts |
Posted - 2008-01-10 : 17:47:33
|
I just happened upon this today, took me a while to debug! my formerly high opinion of select is now lessened...declare @i int, @j intset @i = 12; set @j = 12;-- now set @i with a select that is not satisfied, -- it's value is still 12 after the select!select @i=id from sysobjects where 1=0 -- @j is set to null here, as expectedset @j=(select id from sysobjects where 1=0) select @i,@j elsasoft.org |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2008-01-10 : 18:05:00
|
They work the way I would expect.The first select returned zero rows, so it did not change @i.Notice that value for @j is NULL if you use SET or SELECT in the code below. The reason is that a subquery returns a scalar, not a rowset, so it will be null.declare @j intset @j = 12;select @j=(select id from sysobjects where 1=0) select [@j]=@jset @j = 12;set @j=(select id from sysobjects where 1=0) select [@j]=@jResults:@j ----------- NULL(1 row(s) affected)@j ----------- NULL(1 row(s) affected) CODO ERGO SUM |
 |
|
|
jezemine
Master Smack Fu Yak Hacker
2886 Posts |
Posted - 2008-01-10 : 18:59:41
|
eh. i guess i have to agree, but i don't like it! i know of no other language where you can turn an assignment into a noop in this way, and leave the operand unaffected. it simply rubs me the wrong way.  elsasoft.org |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2008-01-10 : 19:47:13
|
| Hehe.. If you want to think of it in terms of other languages; think of it in terms of short circuiting. :)[url]http://www.mathworks.com/access/helpdesk/help/techdoc/index.html?/access/helpdesk/help/techdoc/ref/logicaloperatorsshortcircuit.html[/url] |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-01-11 : 01:52:39
|
quote: Originally posted by jezemine eh. i guess i have to agree, but i don't like it! i know of no other language where you can turn an assignment into a noop in this way, and leave the operand unaffected. it simply rubs me the wrong way.  elsasoft.org
I think the same applies to other DBMSs as well Also refer http://sqlblog.com/blogs/denis_gobo/archive/2007/10/24/3079.aspxMadhivananFailing to plan is Planning to fail |
 |
|
|
jezemine
Master Smack Fu Yak Hacker
2886 Posts |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2008-01-11 : 09:07:11
|
| why does that rub you the wrong way?those 2 statements you run are completly different._______________________________________________Causing trouble since 1980blog: http://weblogs.sqlteam.com/mladenpSSMS Add-in that does a few things: www.ssmstoolspack.com <- new version out |
 |
|
|
jezemine
Master Smack Fu Yak Hacker
2886 Posts |
Posted - 2008-01-11 : 09:52:50
|
they are different, but not completely different. they both act the same when there is a match.I guess the root of my problem with it is that this is even allowed in SQL:select @i=id from mytable where...as MVJ says, the select returns a rowset, but @i is a scalar. they are different types. so the fact that you are allowed to equate them is irritating to me. I come from a background of C/C++/C# so that's my perspective. You can't do this in C# for example:int i=0;int[] j = new int[10];i = j; // oops!seems like the most you should be allowed to do in sql is this:select top 1 @i=id from mytable where...that way you are equating two similar types. even in this case though, @i wouldn't be set to NULL when there's no match...In the end I guess I just wasn't expecting this behavior. If it's not surprising to anyone else, that's ok.  elsasoft.org |
 |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2008-01-11 : 10:01:01
|
| It's a sql serverextension.A consequence of allowingselect @i = @i + id from mytable(andupdate tblset i = @i, @i = i+1)to loop through the setconsiderselect @i = @i + id from mytableif you change that toselect @i = id from mytableit becomes obvious what it will do.Not saying this is a good thing but it is a useful feature.==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
|