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 Tuesday, January 5, 2010 7:50 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Monday, July 21, 2014 8:35 AM
Points: 3,642, Visits: 72,429
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.





--Mark Tassin
MCITP - SQL Server DBA
Proud member of the Anti-RBAR alliance.
For help with Performance click this link
For tips on how to post your problems
Post #842106
Posted Tuesday, January 5, 2010 8:23 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 12:30 PM
Points: 5,574, Visits: 24,820
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


If everything seems to be going well, you have obviously overlooked something.

Ron

Please help us, help you -before posting a question please read

Before posting a performance problem please read
Post #842132
Posted Tuesday, January 5, 2010 8:55 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Thursday, July 10, 2014 12:49 PM
Points: 1,676, Visits: 1,754
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
Post #842157
Posted Tuesday, January 5, 2010 10:10 AM


SSC-Insane

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

Group: General Forum Members
Last Login: Today @ 2:43 PM
Points: 21,338, Visits: 15,006
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


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Posting Data Etiquette - Jeff Moden
Hidden RBAR - Jeff Moden
VLFs and the Tran Log - Kimberly Tripp
Post #842211
Posted Tuesday, January 5, 2010 10:11 AM


SSC-Insane

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

Group: General Forum Members
Last Login: Today @ 2:43 PM
Points: 21,338, Visits: 15,006
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


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Posting Data Etiquette - Jeff Moden
Hidden RBAR - Jeff Moden
VLFs and the Tran Log - Kimberly Tripp
Post #842212
Posted Tuesday, January 5, 2010 10:12 AM


SSC-Insane

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

Group: General Forum Members
Last Login: Today @ 2:43 PM
Points: 21,338, Visits: 15,006
ziangij (1/4/2010)
option C doesn't work, it throws an error while executing in sql 2005.


You will need to change the Employees table in the variable to a valid table in your database.




Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Posting Data Etiquette - Jeff Moden
Hidden RBAR - Jeff Moden
VLFs and the Tran Log - Kimberly Tripp
Post #842213
Posted Tuesday, January 5, 2010 10:30 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Yesterday @ 7:26 AM
Points: 1,843, Visits: 280
CirquedeSQLeil (1/5/2010)
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.


I found my problem with the quotename. I used the AdventureWorks Database and pointed it to the Employee Table. The Employee table has a schema of Humanresources. When the quotename encloses the entire table [Humanresources.Employee] this throws the error. The correct syntax for that being [Humanresources].[Employee]. I changed to a different db and chose a table with dbo schema and thus it works fine on SQL2008.
Post #842237
Posted Tuesday, January 5, 2010 10:36 AM


SSC-Insane

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

Group: General Forum Members
Last Login: Today @ 2:43 PM
Points: 21,338, Visits: 15,006
rustman (1/5/2010)
CirquedeSQLeil (1/5/2010)
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.


I found my problem with the quotename. I used the AdventureWorks Database and pointed it to the Employee Table. The Employee table has a schema of Humanresources. When the quotename encloses the entire table [Humanresources.Employee] this throws the error. The correct syntax for that being [Humanresources].[Employee]. I changed to a different db and chose a table with dbo schema and thus it works fine on SQL2008.


That makes sense. That might be a good suggestion for updating this question with - alter it to work with different schemas.




Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Posting Data Etiquette - Jeff Moden
Hidden RBAR - Jeff Moden
VLFs and the Tran Log - Kimberly Tripp
Post #842242
Posted Wednesday, January 6, 2010 12:17 AM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Tuesday, May 6, 2014 3:51 AM
Points: 317, Visits: 57
I changed the table name, Now it works fine in 2005. good Question!
Thanks to Sudhir.
Post #842568
Posted Wednesday, January 6, 2010 5:02 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Tuesday, November 5, 2013 6:08 AM
Points: 1,079, Visits: 591
Beaten but i learned 1 or 2 things

What you don't know won't hurt you but what you know will make you plan to know better
Post #842686
« Prev Topic | Next Topic »

Add to briefcase ««1234»»»

Permissions Expand / Collapse