|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Wednesday, January 16, 2013 4:11 AM
Points: 1,569,
Visits: 56
|
|
| Comments posted to this topic are about the item Dynamic SQL
|
|
|
|
|
SSCoach
         
Group: General Forum Members
Last Login: Yesterday @ 10:25 AM
Points: 18,754,
Visits: 12,337
|
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Friday, May 17, 2013 2:53 AM
Points: 1,530,
Visits: 359
|
|
option C doesn't work, it throws an error while executing in sql 2005.
|
|
|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Monday, October 22, 2012 7:08 AM
Points: 232,
Visits: 112
|
|
It works fine in 2005. good Question!
Thanks,
Ashka Modi Software Engineer || credEcard Technologies (india) Pvt. Ltd.
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Monday, May 16, 2011 7:40 AM
Points: 106,
Visits: 13
|
|
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
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Wednesday, May 22, 2013 10:36 AM
Points: 1,405,
Visits: 253
|
|
I couldn't see the answer to this question, and so I guessed "C" which happened to be correct.
However, when I tried the three options on my system, "C" returned an error, "B" also returned an error, and "A" returned NULL. I suspect that my errors had a similar cause to that reported by others. This leads me to make a suggestion for consideration by the adjudicators (if there are any!):
I suggest that before allowing a question to be used, the submitter must ensure that all assumptions and pre-conditions for the question be stated clearly. These assumptions would include such items as database names, table existence, as well as any security or user-privilege features necessary to ensure that the correct response would be produced when the query or procedure is run. I have noted many instances of reports in this forum when queries have failed (and have had some such myself), and I feel that this shows poor appreciation by the submitter that not every system resembles his own.
Happy New Year to one and all!
Ken.
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Friday, January 11, 2013 8:20 AM
Points: 1,608,
Visits: 373
|
|
Cool question! Taught me about a new way to execute dynamic SQL. Thank you Sudhir.
To all who reported troubles: It works perfectly on my instance of SS2005. What messages did you receive?
Best regards, Dietmar Weickert.
|
|
|
|
|
SSC-Insane
         
Group: General Forum Members
Last Login: Today @ 9:24 AM
Points: 21,631,
Visits: 27,488
|
|
kaspencer (1/5/2010) I couldn't see the answer to this question, and so I guessed "C" which happened to be correct.
However, when I tried the three options on my system, "C" returned an error, "B" also returned an error, and "A" returned NULL. I suspect that my errors had a similar cause to that reported by others. This leads me to make a suggestion for consideration by the adjudicators (if there are any!):
I suggest that before allowing a question to be used, the submitter must ensure that all assumptions and pre-conditions for the question be stated clearly. These assumptions would include such items as database names, table existence, as well as any security or user-privilege features necessary to ensure that the correct response would be produced when the query or procedure is run. I have noted many instances of reports in this forum when queries have failed (and have had some such myself), and I feel that this shows poor appreciation by the submitter that not every system resembles his own.
Happy New Year to one and all!
Ken.
I disagree. If you want to run the code in your environment, you should take the appropriate steps based on the code to ensure that it runs. I ran the code in a sandbox database where I had to make appropriate changes, including commenting code as I ran each option to see what occurred.
The reason is the same as you mentioned, the person providing a QotD can't know how everyone's environment is setup. Do you have a AdventureWorks installed on your servers at work? I don't. Do you have your databases on your D: drive, or your backup files on the E: drive? There are so many possibilities that it isn't possible for a person to ensure that the code will work on everyones instance of SQL Server.
After answering the question, I tested the code.
First option, returned null. Second option, an error. Third option, correct result based on the table I used in the original modification for the table to query.
All of that confirmed my original answer of C.
 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)
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Wednesday, May 01, 2013 7:27 AM
Points: 905,
Visits: 8,640
|
|
Like others, I answered A because it returned "the expected output": NULL. Option C returns an error:
Msg 208, Level 16, State 1, Line 1 Invalid object name 'dbo.Employees'.
I didn't put in the name of a valid table name in my database because that was not a part of the question. The question had the following as a "prequel" to all three of the queries:
declare @tablevariable varchar(100) set @tablevariable = 'Employees'
So, for option C, I executed
declare @tablevariable varchar(100) set @tablevariable = 'Employees'
DECLARE @sql nvarchar(4000), @params nvarchar(4000), @count int SELECT @sql = N' SELECT @cnt = COUNT(*) FROM dbo.' + quotename(@tablevariable) SELECT @params = N'@cnt int OUTPUT' EXEC sp_executesql @sql, @params, @cnt = @count OUTPUT select @count
Therefore, option A returns the expected result (since the table variable is empty) and should be a correct answer.
Steve Eckhart
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Monday, May 20, 2013 1:06 PM
Points: 1,417,
Visits: 255
|
|
| My SQL2008 system seems to have trouble with the 'quotename' which encloses the tablename in []. Remove that with the dbo in front of the table name and Option C works fine.
|
|
|
|