THE SQL Server Blog Spot on the Web

Welcome to - The SQL Server blog spot on the web Sign in | |
in Search

Denis Gobo

SQL Teaser: Select

Without running this code try to guess what the values of @var1 and @var2 will be

CREATE TABLE #TeasMeNot(id int)

DECLARE @var1 int,@var2 int

SELECT @var1=0,@var2 = 0

SELECT @var1 =id FROM #TeasMeNot

SELECT @var2 = (SELECT id FROM #TeasMeNot)

--What is the value of @var1 and @var2?

SELECT @var1,@var2



Published Wednesday, October 24, 2007 3:24 PM by Denis Gobo
Filed under:



dbaduck said:

@var1 should = 0 and @var2 should be NULL because @var1 = 0 in the beginning and there are no rows in the table so it will remain 0 and @var2 is = 0 in the beginning but the subquery returns NULL because it is not part of the original query so @var2 gets NULL.

Verified after running the code.  Great teaser Denis.

October 24, 2007 3:23 PM

PiRX said:

On first sight it looked like both values get set to NULL. As there is teaser in title it couldn't be that simple so I thought about again and guessed right (@var1=0, @var2 IS NULL)

October 26, 2007 1:21 AM
New Comments to this post are disabled

About Denis Gobo

I was born in Croatia in 1970, when I was one I moved to Amsterdam (and yes Ajax is THE team in Holland) and finally in 1993 I came to the US. I have lived in New York City for a bunch of years and currently live in Princeton, New Jersey with my wife and 3 kids. I work for Dow Jones as a Database architect in the indexes department, one drawback: since our data goes back all the way to May 1896 I cannot use smalldates ;-( I have been working with SQL server since version 6.5 and compared to all the other bloggers here I am a n00b. Some of you might know me from or even from some of the newsgroups where I go by the name Denis the SQL Menace If you are a Tek-Tips user then you might know me by the name SQLDenis, I am one of the guys answering SQL Questions in the SQL Programming forum.

This Blog


Privacy Statement