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


Dynamic SQL


Dynamic SQL

Author
Message
john.curran.z3g
john.curran.z3g
SSC Veteran
SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)

Group: General Forum Members
Points: 264 Visits: 53
I chose C because there wasn't a 'D - None of the above'.

My instance of SQL 2005 coughs with the error:

Msg 208, Level 16, State 1, Line 1
Invalid object name 'dbo.Employees'.
and returns NULL as a result.
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (95K reputation)SSC Guru (95K reputation)SSC Guru (95K reputation)SSC Guru (95K reputation)SSC Guru (95K reputation)SSC Guru (95K reputation)SSC Guru (95K reputation)SSC Guru (95K reputation)

Group: General Forum Members
Points: 95337 Visits: 38968
john.curran.z3g (1/6/2010)
I chose C because there wasn't a 'D - None of the above'.

My instance of SQL 2005 coughs with the error:

Msg 208, Level 16, State 1, Line 1
Invalid object name 'dbo.Employees'.
and returns NULL as a result.


Try it after you change the table name in the variable to a table that exists in the database you are running the code.

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)
Dan Guzman - Not the MVP
Dan Guzman - Not the MVP
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1338 Visits: 751
I have to laugh,

After seeing all these comments about what should have been implied and what was inferred, I can't tell you the number of times these QotD have been trick questions, trying to 'get you' with slight and small syntax errors or db assumptions, intentionally generating an error message.

Just because someone ran the code and got an error message does not mean the error message was not the correct answer.

In this case, an error message was not option 'D', so I proceeded with the assumption that at least one of the three generated correct results.

Thanks for the question Sudhir. I enjoyed this one.
JF1081
JF1081
SSC-Addicted
SSC-Addicted (490 reputation)SSC-Addicted (490 reputation)SSC-Addicted (490 reputation)SSC-Addicted (490 reputation)SSC-Addicted (490 reputation)SSC-Addicted (490 reputation)SSC-Addicted (490 reputation)SSC-Addicted (490 reputation)

Group: General Forum Members
Points: 490 Visits: 322
Steve Eckhart (1/5/2010)
Like others, I answered A because it returned "the expected output": NULL. Option C returns an error:


I am guilty of copying and pasting when I do not know an answer, but I don't get mad if I don't get my points becasue I obviously didn't know the answer anyway.
Tom Thomson
Tom Thomson
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26047 Visits: 12499
OK, I answered C because A and B are so completely wrong that C is the only option where we can reasonably guess that the setter of the question got it wrong enough to think C would work. So I got my point - for understanding the sort of stupid error that the question setter would make, not for producing a correct answer (because "(D) none o the above" is the correct answer, but not available). This is a really bloody awful question and answer.
I'm seeing far too much of thios sort or thing in QOD. Presumably it's completely unmoderated?

Tom

Tom Thomson
Tom Thomson
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26047 Visits: 12499
Rupal Raval (1/5/2010)
what error does it throw?
have you written

'declare @tablevariable varchar(100)
set @tablevariable = 'any valid table name'

Before code C?

map @tablevariable to one of your database table, select that database as current database and then execute the code


No, and it wouldn't help if I did. I don't have a table called 'any valid table name' in any database on my server. (I know that sounds nasty - but the provocation was extreme!)

Claiming that this is a sensible question with a sensible answer because it's obvious that unless you change the question all the answers are wrong is just nonsense. Yes, there are real life situations like that (the requirement as provided is nonsense), but in those situations you should not guess what part of the question (requirement) you have to change, you should throw the thing back at whoever provided the question and point out that it's nonsense. Otherwise you risk finding yourself spending a lot of effort on doing something completely useless, because you guessed wrong. And in practise, no matter how obvious it looks to you what the required modification is, you are NOT the person who understands the business inside out and specifies the requirement, so your guess will often be wrong. (If you were the person with that knowledge and understanding, you would be writing the requirement, and would not make silly errors like that - I hope.)

Tom

Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (95K reputation)SSC Guru (95K reputation)SSC Guru (95K reputation)SSC Guru (95K reputation)SSC Guru (95K reputation)SSC Guru (95K reputation)SSC Guru (95K reputation)SSC Guru (95K reputation)

Group: General Forum Members
Points: 95337 Visits: 38968
Tom.Thomson (2/9/2010)
OK, I answered C because A and B are so completely wrong that C is the only option where we can reasonably guess that the setter of the question got it wrong enough to think C would work. So I got my point - for understanding the sort of stupid error that the question setter would make, not for producing a correct answer (because "(D) none o the above" is the correct answer, but not available). This is a really bloody awful question and answer.
I'm seeing far too much of thios sort or thing in QOD. Presumably it's completely unmoderated?


Okay, explain to me why C, although the correct answer, is wrong.

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 (95K reputation)SSC Guru (95K reputation)SSC Guru (95K reputation)SSC Guru (95K reputation)SSC Guru (95K reputation)SSC Guru (95K reputation)SSC Guru (95K reputation)SSC Guru (95K reputation)

Group: General Forum Members
Points: 95337 Visits: 38968
Tom.Thomson (2/9/2010)
Rupal Raval (1/5/2010)
what error does it throw?
have you written

'declare @tablevariable varchar(100)
set @tablevariable = 'any valid table name'

Before code C?

map @tablevariable to one of your database table, select that database as current database and then execute the code


No, and it wouldn't help if I did. I don't have a table called 'any valid table name' in any database on my server. (I know that sounds nasty - but the provocation was extreme!)

Claiming that this is a sensible question with a sensible answer because it's obvious that unless you change the question all the answers are wrong is just nonsense. Yes, there are real life situations like that (the requirement as provided is nonsense), but in those situations you should not guess what part of the question (requirement) you have to change, you should throw the thing back at whoever provided the question and point out that it's nonsense. Otherwise you risk finding yourself spending a lot of effort on doing something completely useless, because you guessed wrong. And in practise, no matter how obvious it looks to you what the required modification is, you are NOT the person who understands the business inside out and specifies the requirement, so your guess will often be wrong. (If you were the person with that knowledge and understanding, you would be writing the requirement, and would not make silly errors like that - I hope.)


Explain to me why you think each of the answers is wrong.

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)
Tom Thomson
Tom Thomson
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26047 Visits: 12499
Lynn Pettis (2/9/2010)

Okay, explain to me why C, although the correct answer, is wrong.

Becasue all the code options povide produce an error or NULL. Now if you think that an error or NULL is the expected result, then one of them may be right and it might be C. I thought that the expected result was some useful data, not an error message or NULL, so that none of the suggested answers produced the expected result. I may have been wrong - maybe case C produced the expected error message, in which case I have to ask why that error message rather than the result of A or B is the expected result. Do you have an answer for that? Or are you taking the rather arrogant view that because you don't like the question you will reformulate it to mean something different (perhaps something more fully specified) rather than refer it back to the poser?

Tom

Tom Thomson
Tom Thomson
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26047 Visits: 12499
Lynn Pettis (2/9/2010)
Explain to me why you think each of the answers is wrong.

Because each answer tries to select from a table with a particular name in an completely unspecified database. That table probably doesn't exist in most databases (it doesn't exist in any DB on any server I ever controlled - and I'll leave you to guess whether that means that I never controlled an HR or Payroll DB or that the table just happened to be named "seirbhiseach" or "Angestellte" instead of "employee".

In the real world it's no good doing what the commenter to whom I was replying suggested - just pick any database you have and then pick any table you have in that DB and then make the query refer to that table - whoever produced the requirement had something in mind, and surely it wasn't to let the developer pick the database and table at random. OK, so here we are talking about QOD instead of about real requirements, and the standard of questions is sometimes pretty low so adjusting the question might often make sense - but as a general rule I think we should try to treat the questions as if they are hard and fast, not subject to adjustment without obtaining agreement from the poser, because in the real world all the badly formulated (and often completely wrong-headed) requirements we see have to go through that negotiate with the poser process.

Tom

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