Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Stairway to T-SQL: Beyond The Basics Level 9: Dynamic T-SQL Code


Stairway to T-SQL: Beyond The Basics Level 9: Dynamic T-SQL Code

Author
Message
Greg Larsen
Greg Larsen
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1051 Visits: 290
Comments posted to this topic are about the item Stairway to T-SQL: Beyond The Basics Level 9: Dynamic T-SQL Code

Gregory A. Larsen, MVP
h.tobisch
h.tobisch
SSC-Enthusiastic
SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)

Group: General Forum Members
Points: 133 Visits: 248
Hi Greg,
I do not go quite with your conclusions.
If parameterized SQL is used as it is intended, the skeleton of the statement @CMD provided by the application and user input used only for parameters,
there will be no danger of SQL injection and consequently no advantage in avoiding dynamic SQL.
Concluding from what I know and what is stated in the article.
reagrds
Herbert
timwell
timwell
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1177 Visits: 2902
Hello Greg,
Another good article. I have enjoyed this stairway series.

In this article I noticed a few inconsistencies that may be from changing your examples?

Several places you use "DELETE" where maybe it should be "DROP" or "DROP TABLE"?
You refer in some captions to a stored procedure called "GetUserName" and in the text to an @EMAIL parameter
Also it looks like this sentence: "In this example I deleted the Client table." should read "In this example I dropped the Product table."
fregatepllada
fregatepllada
Valued Member
Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)

Group: General Forum Members
Points: 66 Visits: 322
Greg, thank you for article.

Could you please refrain from providing SELECT * FROM table example - it's a proverbial ticking "time bomb".
Changes in table schema like creating new or dropping existing column could trigger a chain reaction of bug further down stream in middle-tier and/or on UI. My favorite anti-pattern - combining SELECT * with querying system views EVERY time somebody try to retrieve data. in this scenario even renaming a column could be a potential problem.
Robert.Sterbal
Robert.Sterbal
SSC Veteran
SSC Veteran (209 reputation)SSC Veteran (209 reputation)SSC Veteran (209 reputation)SSC Veteran (209 reputation)SSC Veteran (209 reputation)SSC Veteran (209 reputation)SSC Veteran (209 reputation)SSC Veteran (209 reputation)

Group: General Forum Members
Points: 209 Visits: 2000
Do the cursors in the example of looping dynamic sql act as an implicit go statement?
Eirikur Eiriksson
Eirikur Eiriksson
SSCertifiable
SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)

Group: General Forum Members
Points: 6714 Visits: 17699
Robert.Sterbal (7/24/2014)
Do the cursors in the example of looping dynamic sql act as an implicit go statement?


Yes, unless it is wrapped in an explicit transaction.
Cool
Eirikur Eiriksson
Eirikur Eiriksson
SSCertifiable
SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)

Group: General Forum Members
Points: 6714 Visits: 17699
Thanks Gregory for this nice peace, good job!
Missed seeing one essential thing though in the injection part and that is a strong coupling to the metadata. If a user can select in any form the objects (table/view/procedure etc.) to use, one should do a firm check for it's existence. A quick sample would be
Cool
DECLARE @SCHEMA  NVARCHAR(128) = N'dbo';
DECLARE @TABLE NVARCHAR(128) = N'CLIENT';
DECLARE @COLUMN NVARCHAR(128) = N'CLIENT_NAME';
DECLARE @SQL_STR NVARCHAR(MAX) = N''

SELECT
@SQL_STR = N'SELECT ' +
COLUMN_NAME
+ N' FROM ' + TABLE_SCHEMA
+ NCHAR(46) + TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME = @COLUMN
AND TABLE_NAME = @TABLE
AND TABLE_SCHEMA = @SCHEMA
--- and so on


Luis Cazares
Luis Cazares
SSCrazy Eights
SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)

Group: General Forum Members
Points: 8530 Visits: 18130
To prevent SQL Injection when the names of objects or columns are entered by the user, I'd recommend as well QUOTENAME() function. Of course, that doesn't remove the good practice of using parametrized queries. Combining both options, you should be safe. If anyone prove me wrong, we can all learn some more.


DECLARE @Table varchar(128) = 'sys.tables; '' SELECT TOP 100* FROM sys.columns'
DECLARE @SQL nvarchar(4000)
SET @SQL = 'SELECT * FROM ' + QUOTENAME(@Table)
PRINT @SQL
EXEC sp_executesql @SQL




Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
gogniben
gogniben
Grasshopper
Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)

Group: General Forum Members
Points: 14 Visits: 24
Thanks for another great article.
Iwas Bornready
Iwas Bornready
SSCrazy Eights
SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)

Group: General Forum Members
Points: 8664 Visits: 885
Good article. We just try to avoid it altogether. We have some and are in the process of getting rid of it.
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