January 4, 2010 at 9:47 pm
Comments posted to this topic are about the item Dynamic SQL
January 4, 2010 at 10:33 pm
Thanks for the question.
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
January 4, 2010 at 10:42 pm
option C doesn't work, it throws an error while executing in sql 2005.
January 5, 2010 at 12:17 am
It works fine in 2005. good Question!
Thanks,
Ashka Modi
Software Engineer || credEcard Technologies (india) Pvt. Ltd.
January 5, 2010 at 12:20 am
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
January 5, 2010 at 2:50 am
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.
You never know: reading my book: "All about your computer" might just tell you something you never knew!
lulu.com/kaspencer
January 5, 2010 at 3:10 am
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.
January 5, 2010 at 6:34 am
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.
January 5, 2010 at 7:38 am
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.
January 5, 2010 at 7:46 am
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.
January 5, 2010 at 7:50 am
Sorry,
I have to agree with Lynn.
If you don't have a table named employees, then of course it won't work.
Copying and pasting the query code into SSMS and running it is the cheap way out.
Knowing you don't have a table named employees in whatever scratch database you have, substituting in the table name in the beginning of the question with one you do have in your test database would let you test it properly.
set @tablevariable = 'Employees'
Notice the beginning of the question.
Sam has to run a query dynamically & get the count in a variable and do some processing based on the count. Which of the following queries will return expected output?
If you just wanted to run the query, you'd need to substitute in a table in place of the employees one that Sam apparently has.
January 5, 2010 at 8:23 am
kaspencer
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.
Why kaspencer, why -- so a user can cut and paste ... that is a heck of a way for a person to test their knowledge ...
Now if all you want is a method to inflate your ego ... then your suggestion would be appropriate ... but to test your knowledge inappropriate
Sudhir Chawal A darn good question
January 5, 2010 at 8:55 am
bitbucket-25253 (1/5/2010)
Why kaspencer, why -- so a user can cut and paste ... that is a heck of a way for a person to test their knowledge ...Now if all you want is a method to inflate your ego ... then your suggestion would be appropriate ... but to test your knowledge inappropriate
Sudhir Chawal A darn good question
Exactly!. The whole point of the QoD is to test one's own knowledge, and the best way to do it is to try figuring the correct answer before pasting anything into the editor. Once the answer is submitted then it is OK to play with the sample statements and adjust them if necessary depending on one's settings. Today's question is a very good one indeed. It shows that sp_executesql is far more powerful than simple exec as it allows designing reusable dynamic SQL queries with multiple parameters in either direction (in or out) and possibly useful plan storage. It should always be used with caution as dynamic SQL can be subjected to injection attacks, but other than that the sp_executesql is a great feature.
Oleg
January 5, 2010 at 10:10 am
I agree with Lynn,Mark and Bitbucket. The idea is to test the code against your system using your knowledge. If that means changing a variable - then change it. Not every database will have an Employees table - that was not the actual question. The question was to test how to get appropriate results from one of the Dynamic SQL statements.
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
January 5, 2010 at 10:11 am
rustman (1/5/2010)
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.
Very interesting. I ran it on my SQL 2k8 database with absolutely no problem.
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
Viewing 15 posts - 1 through 15 (of 32 total)
You must be logged in to reply to this topic. Login to reply