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»»»

Predict output Expand / Collapse
Author
Message
Posted Wednesday, January 20, 2010 3:24 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, September 18, 2014 7:58 AM
Points: 1,388, Visits: 274
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.
Post #850341
Posted Wednesday, January 20, 2010 3:27 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Thursday, September 18, 2014 2:51 AM
Points: 1,743, Visits: 485
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.
Post #850346
Posted Wednesday, January 20, 2010 5:59 AM
SSC-Enthusiastic

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

Group: General Forum Members
Last Login: Tuesday, August 19, 2014 9:34 AM
Points: 136, Visits: 141
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....
Post #850403
Posted Wednesday, January 20, 2010 6:40 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 7:22 PM
Points: 20,680, Visits: 32,279
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.




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)
Post #850433
Posted Wednesday, January 20, 2010 6:43 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 7:22 PM
Points: 20,680, Visits: 32,279
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.



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)
Post #850436
Posted Wednesday, January 20, 2010 7:15 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, March 8, 2013 8:32 AM
Points: 26, Visits: 41
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.
Post #850467
Posted Wednesday, January 20, 2010 7:28 AM


SSC-Enthusiastic

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

Group: General Forum Members
Last Login: Thursday, August 21, 2014 2:27 PM
Points: 170, Visits: 231
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?


Kev -=Conan The Canadian=-
@ConanTheCdn
Post #850479
Posted Wednesday, January 20, 2010 7:36 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 1:09 PM
Points: 2,074, Visits: 2,068
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

Post #850489
Posted Wednesday, January 20, 2010 7:39 AM


SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Yesterday @ 2:24 PM
Points: 27, Visits: 471
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
Post #850492
Posted Wednesday, January 20, 2010 7:49 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 7:22 PM
Points: 20,680, Visits: 32,279
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.



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)
Post #850501
« Prev Topic | Next Topic »

Add to briefcase ««12345»»»

Permissions Expand / Collapse