SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Predict output


Predict output

Author
Message
SQLTuna
SQLTuna
UDP Broadcaster
UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)

Group: General Forum Members
Points: 1470 Visits: 328
vk-kirov (1/20/2010)
mzz3lh (1/20/2010)
However, when I ran this to try it out, I got an error:
(1 row(s) affected)

(1 row(s) affected)
this is an exception
Msg 208, Level 16, State 0, Line 10
Invalid object name '#temp'.

Probably the IMPLICIT_TRANSACTIONS option is turned on.
Try this code:
SET IMPLICIT_TRANSACTIONS OFF
GO
... here comes the original script ...



Good call, cheers. I had it switched on without realising, so the create table statement was obviously not autocommitting.

I still think the options in the question were wrong though, even after running the code successfully. The third option implied the 'this is an exception' string formed part of the table data, when clearly it was a printed message.
kaspencer
kaspencer
SSCrazy
SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)

Group: General Forum Members
Points: 2672 Visits: 858
OK OK OK, so here I am once again complaining!

When I studied this question, I thought: "Well, the only values inserted into the table, will be 1, 2, 4 and 5. So, before going to the webpage to select my answer, I expected to see "1, 2, 4, 5" as an option.

But when I got to the website, there was no such option available, but there was one where the third value on the list was a string 'This is an exception'. I selected that option as a compromise, and got the points.

"BUT", I thought, "this string can't be inserted into the database, although it will be in the message list." So I had selected the option which would be in my message list rather than the output of the query, which wasn't shown as an option anyway. Of course I had gained the two points but I wasn't happy with the options given.

And the moral of this story: would future question submittees please distinguish between message output and query output. Or better still would the question editors (although there don't seem to be any editors anyway) please check the quality of questions and use a little editorial blue-pencil where necessary.

All the best

Ken.

You never know: reading my book: "All about your computer" might just tell you something you never knew!
lulu.com/kaspencer
Snehalatha Mannar
Snehalatha Mannar
SSC-Enthusiastic
SSC-Enthusiastic (166 reputation)SSC-Enthusiastic (166 reputation)SSC-Enthusiastic (166 reputation)SSC-Enthusiastic (166 reputation)SSC-Enthusiastic (166 reputation)SSC-Enthusiastic (166 reputation)SSC-Enthusiastic (166 reputation)SSC-Enthusiastic (166 reputation)

Group: General Forum Members
Points: 166 Visits: 175
the given answers are incorrect....when i execute the code(SQL 2005)....it gives me the result as 1,2,4,5 and this is an exception message. It is annoying to see incorrect options....;-)
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)

Group: General Forum Members
Points: 98524 Visits: 38996
mzz3lh (1/20/2010)
I have to agree here. None of the answers match what I would expect, which would be 'This is an exception' in the message pane, followed by the contents of the temp table: 1, 2, 4, 5.

However, when I ran this to try it out, I got an error:
(1 row(s) affected)

(1 row(s) affected)
this is an exception
Msg 208, Level 16, State 0, Line 10
Invalid object name '#temp'.


Which indicates that despite the try-catch block, the error caused the temp table to be dropped mid-execution.


Or that something was wrong with the code you ran. It ran fine for me, and it appears from a few other posts that it ran fine for others.


I agree, the answers may not have been displayed as many of us have seen it when we ran the code. The question is, were you able to correctly predict the output? In this case, as mentioned in another post, there really was only one option that could be correct. It may have been more difficult had there been one or two more possible answers with the exception message included.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)

Group: General Forum Members
Points: 98524 Visits: 38996
To all those who are expecting PRECISELY CORRECT ANSWERS, how about putting yourself out there and submit some questions. The answers may not have been precise, but I don't feel that it really took that much away from the question. What was important was seeing that using the TRY/CATCH kept the while loop from being aborted, allowing you to control the processing within SQL itself.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
nomlad
nomlad
SSC Rookie
SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)

Group: General Forum Members
Points: 46 Visits: 45
It was fairly easy to see the concept which was being put across here regarding try/catch blocks - not exactly a new one for most developers these days I should imagine. But if I'd posed that question in an interview and the candidate had given the answer given as correct I would not have marked it as correct.
The answer given as correct implies that the text of the error message was somehow inserted into the table and then returned as part of the result set - completely wrong.
Conan The Canadian
Conan The Canadian
Old Hand
Old Hand (381 reputation)Old Hand (381 reputation)Old Hand (381 reputation)Old Hand (381 reputation)Old Hand (381 reputation)Old Hand (381 reputation)Old Hand (381 reputation)Old Hand (381 reputation)

Group: General Forum Members
Points: 381 Visits: 257
Lynn Pettis (1/20/2010)
To all those who are expecting PRECISELY CORRECT ANSWERS, how about putting yourself out there and submit some questions. The answers may not have been precise, but I don't feel that it really took that much away from the question. What was important was seeing that using the TRY/CATCH kept the while loop from being aborted, allowing you to control the processing within SQL itself.


Here I was going to complain about the correct answer not being one of the options but I was beaten to it several times over!

The problem with the correct answer not being one of the options is that it confuses junior and intermediate level folks (I would consider myself intermediate as there is a ton about SQL Server that I don't know.)

By the way, is it just me, or is your signature getting longer? Hehe

Kev -=Conan The Canadian=-
@ConanTheCdn
Dave62
Dave62
Hall of Fame
Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)

Group: General Forum Members
Points: 3585 Visits: 2836
Lynn Pettis (1/20/2010)
... What was important was seeing that using the TRY/CATCH kept the while loop from being aborted, allowing you to control the processing ...


I totally agree. This was a great example of how to loop and do conditional processing with error handling and without a cursor.

Excellent question, I made some edits and comments and have saved it for future reference.


declare @i int, @j int

set @i = 1

create table #temp (id int)
while (@i<=5)
begin -- while loop
begin try -- trap errors
begin transaction
if (@i = 3)
set @j = @i/0

insert into #temp values (@i) -- insert values when there are no errors
print '@i = [' + Convert(varChar(8), @i) + ']'
commit transaction
end try

begin catch -- handle errors
rollback transaction
print 'An error was caught when @i = [' + Convert(varChar(8), @i) + ']';
end catch

set @i = @i + 1
end -- while loop

select * from #temp

drop table #temp


jswong05
jswong05
Right there with Babe
Right there with Babe (715 reputation)Right there with Babe (715 reputation)Right there with Babe (715 reputation)Right there with Babe (715 reputation)Right there with Babe (715 reputation)Right there with Babe (715 reputation)Right there with Babe (715 reputation)Right there with Babe (715 reputation)

Group: General Forum Members
Points: 715 Visits: 476
The original choices do not contain correct answers. Please verify the Q/A before it is published.
Imagine you take a GRE special topic test that involes world wide thousands of thousands people on the same day, and in the middle you found there is a question there has no correct answer. We have to guess what it was asking. This is the test that determine your fate.
Try-catch was invented 20 years ago when I coded C++, it is nice to see people starts understanding it in SQL community.

Jason
http://dbace.us
:-P
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)

Group: General Forum Members
Points: 98524 Visits: 38996
nomlad (1/20/2010)
It was fairly easy to see the concept which was being put across here regarding try/catch blocks - not exactly a new one for most developers these days I should imagine. But if I'd posed that question in an interview and the candidate had given the answer given as correct I would not have marked it as correct.
The answer given as correct implies that the text of the error message was somehow inserted into the table and then returned as part of the result set - completely wrong.


Is it wrong? Look a little closer, did the individual understand how the code itself worked? Maybe the output was formated wrong, but the content correct. You can always work with someone to make sure they have better understanding of how the output would actually be presented, but they would have at least demonstrated an understanding of the concepts. In the case of an interview, I'd have to give partial credit at least.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search