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

Dynamic SQL Expand / Collapse
Author
Message
Posted Monday, January 4, 2010 9:47 PM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Wednesday, November 5, 2014 1:53 AM
Points: 1,569, Visits: 57
Comments posted to this topic are about the item Dynamic SQL
Post #841840
Posted Monday, January 4, 2010 10:33 PM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Today @ 12:08 PM
Points: 17,967, Visits: 15,975
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
Post #841847
Posted Monday, January 4, 2010 10:42 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Sunday, November 2, 2014 11:03 PM
Points: 2,103, Visits: 371
option C doesn't work, it throws an error while executing in sql 2005.
Post #841850
Posted Tuesday, January 5, 2010 12:17 AM


SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Monday, October 22, 2012 7:08 AM
Points: 233, Visits: 112
It works fine in 2005. good Question!

Thanks,

Ashka Modi
Software Engineer || credEcard Technologies (india) Pvt. Ltd.
Post #841874
Posted Tuesday, January 5, 2010 12:20 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-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
Post #841880
Posted Tuesday, January 5, 2010 2:50 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 2:42 AM
Points: 1,791, Visits: 510
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
Post #841925
Posted Tuesday, January 5, 2010 3:10 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Thursday, August 28, 2014 7:17 AM
Points: 1,608, Visits: 374
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.
Post #841934
Posted Tuesday, January 5, 2010 6:34 AM


SSC-Insane

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

Group: General Forum Members
Last Login: Today @ 11:09 AM
Points: 20,806, Visits: 32,738
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)
Post #842026
Posted Tuesday, January 5, 2010 7:38 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Friday, October 31, 2014 9:32 AM
Points: 1,009, Visits: 8,661
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
Post #842098
Posted Tuesday, January 5, 2010 7:46 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 6:58 AM
Points: 1,954, Visits: 287
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.
Post #842103
« Prev Topic | Next Topic »

Add to briefcase 1234»»»

Permissions Expand / Collapse