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


Dynamic SQL


Dynamic SQL

Author
Message
mtassin
mtassin
SSCertifiable
SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)

Group: General Forum Members
Points: 7318 Visits: 72521
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
bitbucket-25253
bitbucket-25253
SSCoach
SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)

Group: General Forum Members
Points: 15935 Visits: 25280
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
Oleg Netchaev
Oleg Netchaev
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2221 Visits: 1817
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
SQLRNNR
SQLRNNR
SSC Guru
SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)

Group: General Forum Members
Points: 67319 Visits: 18570
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

SQLRNNR
SQLRNNR
SSC Guru
SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)

Group: General Forum Members
Points: 67319 Visits: 18570
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

SQLRNNR
SQLRNNR
SSC Guru
SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)

Group: General Forum Members
Points: 67319 Visits: 18570
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, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw

rustman
rustman
SSCrazy
SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)

Group: General Forum Members
Points: 2957 Visits: 361
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.
SQLRNNR
SQLRNNR
SSC Guru
SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)

Group: General Forum Members
Points: 67319 Visits: 18570
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, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw

SRatnaparkhi
SRatnaparkhi
Old Hand
Old Hand (335 reputation)Old Hand (335 reputation)Old Hand (335 reputation)Old Hand (335 reputation)Old Hand (335 reputation)Old Hand (335 reputation)Old Hand (335 reputation)Old Hand (335 reputation)

Group: General Forum Members
Points: 335 Visits: 57
I changed the table name, Now it works fine in 2005. good Question!
Thanks to Sudhir.
BudaCli
BudaCli
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1560 Visits: 598
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
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