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

Stairway to T-SQL: Beyond The Basics Level 9: Dynamic T-SQL Code Expand / Collapse
Author
Message
Posted Tuesday, July 8, 2014 7:26 PM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Friday, June 6, 2014 2:06 PM
Points: 1,040, Visits: 277
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

Need SQL Server Examples check out my website at http://www.sqlserverexamples.com
Post #1590576
Posted Wednesday, July 23, 2014 4:38 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Monday, October 27, 2014 8:32 AM
Points: 54, Visits: 93
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
Post #1595407
Posted Wednesday, July 23, 2014 8:41 AM
Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Today @ 7:28 AM
Points: 508, Visits: 1,393
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."
Post #1595515
Posted Wednesday, July 23, 2014 4:54 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, October 2, 2014 5:17 PM
Points: 35, Visits: 172
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.
Post #1595760
Posted Thursday, July 24, 2014 7:38 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Today @ 8:11 AM
Points: 173, Visits: 1,863
Do the cursors in the example of looping dynamic sql act as an implicit go statement?
Post #1595894
Posted Thursday, July 24, 2014 8:42 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 8:17 AM
Points: 2,252, Visits: 6,164
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.
Post #1595915
Posted Thursday, July 24, 2014 8:48 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 8:17 AM
Points: 2,252, Visits: 6,164
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

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

Post #1595920
Posted Thursday, July 24, 2014 12:17 PM


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: Today @ 9:25 AM
Points: 3,805, Visits: 8,557
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.
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?

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1595982
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse