views:

412

answers:

4

I'm studying for the MCTS 70-433 "Database Design" cert, and in the text that I'm studying, one of the self-tests has this question.

You have a stored procedure named Get_NewProducts. You wish to insert the results of this stored procedure into the Production.Product table and output the INSERTED.* values using the OUTPUT clause. What is the best way to do this?

There are four possible answers. The first three choices are all variations of an "INSERT...OUTPUT...EXECUTE Get_NewProducts" statement. The fourth choice, D, simply says "Rewrite the stored procedure as a table-valued function.".

D is the correct answer. I don't quite understand why, and there is nothing in the text that explains it. Anyone have any insights?

+1  A: 

My knee-jerk reaction to this (I hit it again a few days ago) is:

  • Stored procedures can be and often are nested. Procedure A calls B, which calls C, and so on.
  • The code called by an INSERT...EXECUTE... statement cannot itself contain or reference an INSERT...EXECUTE... statement. If you put one in, you cannot then "embed" this procedure in a later INSERT...EXECUTE...

This may seem trivial, and it generally is, at least until you hit it during a refactoring project. Once bitten, twice shy. (And it's bit me a number of times.)

There are a number of style and appearance reasons as well, but they're kind of superficial. There probably is a serious technical reason, perhaps having to do with recompiles or query execution plans; if so, hopefully someone else will post them.

Philip Kelley
A: 

Just one reason their "right answer" is not right: TVFs have issues with error checking and reporting.

It's a really odd question/answer because D doesn't even seem to be a possibility given the question.

Rob Garrison
A: 

I don't know a 'correct' answer, but I guess that the thinking of the author is that 70-433 Database Development is development and design oriented exam, as opposed to say one of the 'data access' exams like 70-442. During the design phase you should be able to spot faults in the existing system and propose better solutions. The author considers that stored procedure that needs to have its output inserted into a table is better off rewritten as a TVF. You'll find both cons and pros as whether a TVF is better than a proc (insert exec nesting pro, bad error handling con just to start with).

I took some of these exams myself and I found that the exam preparation material and the exams themselves are not always the absolute ultimate reference on their subject. On the large they are correct and good value, but they have problems here and then and I found at least some questionable recommendations and even plain wrong ones. And on the topics that I found to be wrong I actually am the ultimate reference on the subject, they were covering code that I wrote on features I designed...

My advice is to get a feel of what is 'expected' answer and be prepared for it during the actual exam. Given your flair points and your answer I've seen, you are already above the exam level, so just go through the hoops, earn your exam badge and move on.

Remus Rusanu
Thanks for the kind words. I agree with the prep material not being the ultimate reference - I just hate it when a text references something like this that you can't discover from the book itself. To be honest... I'm not too impressed with the text overall... it's the Syngress "Prep Kit" book. Needs another couple of editors to take a look at it IMO.
womp
+5  A: 

Well, from msdn:

" The OUTPUT clause is not supported in DML statements that reference local partitioned views, distributed partitioned views, or remote tables, or *INSERT statements that contain an execute_statement*."

Charles Bretana
Man... it's right on the TSQL reference page too (http://technet.microsoft.com/en-us/library/ms177564.aspx), which I checked. Now that you point it out, it jumps right out at me. Just another case of "read the documentation" I guess :) Thanks!
womp
No, thank you... I've been using SQL since 93/94 (ver 4.2) and I realize I need to read up on all the new stuff that's been added in the last few releases.. I had no idea that Output keyword even existed until I checked the docs researching to answer your question.
Charles Bretana