Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dynamic SQL for the DBA by Jeff Moden

Similar presentations


Presentation on theme: "Dynamic SQL for the DBA by Jeff Moden"— Presentation transcript:

1 Dynamic SQL for the DBA by Jeff Moden
Heat Lightning Dynamic SQL for the DBA by Jeff Moden SPID Lightning Rounds, Detroit, Mi 12 September 2013

2 Your Speaker - Jeff Moden
17 years experience working with SQL Server Mostly Self Taught One of Leading Posters on SQLServerCentral.com More than 33,000 posts (heh… some are even useful) 30+ articles on the “Black Arts” of T-SQL Member since 2003 SQL Server MVP 2008 thru 2013 Winner of the “Exceptional DBA” award for 2011 Lead Application DBA, Data Architect, and SQL Mentor for Proctor Financial, Inc. SQL Server is both my profession and my hobby (Yeah, I know… I need to get a life ;-)

3 DANGER YOU HAVE BEEN WARNED!
DO NOT USE ANY OF THE METHODS IN THIS PRESENTATION FOR PUBLIC FACING CODE BECAUSE THE CODE IS SUBJECT TO SQL INJECTION. THE CONTENT OF ALL VARIABLES MUST BE CHECKED FOR SQL INJECTION BEFORE BEING USED. I ASSUME NO AND CANNOT BE ASSIGNED ANY LIABILITY WHATSOEVER, EITHER EXPRESSED OR IMPLIED, FOR THE USE OF ANY OR ALL OF THIS CODE.

4 Agenda Define a Problem that Requires Dynamic SQL. Build the Code
Build one hardcoded iteration. Witness Some Typical Dynamic SQL. Steps to build Dynamic SQL more quickly and easily. Run the Code

5 Reference Material What SQL Injection Is
Learn What a Tally Table/Function is and How It Replaces Certain Types of Loops: Partitioning and Piecemeal Restores

6 Define the Problem File Level Preparations to Partition Large Tables and Setup to Support “Piecemeal” Restores Which Also Reduces Nightly Backup Requirements. Same Database. 1 Filegroup per Month. 1 File per Filegroup. Resulting .NDF files on a different, less expensive drive. Filegroup and File Names must be in the format of tablename_yyyymmdd

7 Build/Test the “Normal” Code
Code to Build 1 Monthly Partition RAISERROR('Working on FileGroup/File SomeTableName_ ',0,1) WITH NOWAIT; ALTER DATABASE FileGroupTest ADD FILEGROUP SomeTableName_ ; ALTER DATABASE FileGroupTest ADD FILE ( NAME = 'SomeTableName_ ' ,FILENAME = 'C:\FileGroupTest\SomeTableName_ ndf' ,SIZE = 100MB ,FILEGROWTH = 100MB ) TO FILEGROUP SomeTableName_ ; Nice, Neat Code

8 Typical Dynamic SQL A Dynamic Mess Tough to “Get It Right”
= 'RAISERROR(''Working on FileGroup/File ' + '_' + ''',0,1) WITH NOWAIT;' + CHAR(10) + ' ALTER DATABASE ' + ' ADD FILEGROUP ' + '_ ;' + CHAR(10) + ' ALTER DATABASE ' + ' ADD FILE' + CHAR(10) + ' (' + CHAR(10) + ' NAME = ''' + '_' + '''' + CHAR(10) + ' ,FILENAME = ''' + '''' + '_' + '.ndf''' + CHAR(10) + ' ,SIZE = 100MB' + CHAR(10) + ' ,FILEGROWTH = 100MB' + CHAR(10) + ' )' + CHAR(10) + ' TO FILEGROUP ' + '_' + ';' ; Tough to “Get It Right” Tough to Read (and we haven’t even added the typical WHILE loop, yet) Tough to Troubleshoot

9 Quicker, Easier Dynamic SQL
Simple Conversions Using “Tokens” with Search’n’Replace. RAISERROR("Working on FileGroup/File WITH NOWAIT; ALTER DATABASE ADD FILEGROUP ALTER DATABASE ADD FILE ( NAME = ,FILENAME = ,SIZE = 100MB ,FILEGROWTH = 100MB ) TO FILEGROUP ; Single Quotes Replaced by Double Quotes so Not to “Break the Chain”.

10 Wrap It In Single Quotes and add a separator…
' RAISERROR("Working on FileGroup/File WITH NOWAIT; ALTER DATABASE ADD FILEGROUP ALTER DATABASE ADD FILE ( NAME = ,FILENAME = ,SIZE = 100MB ,FILEGROWTH = 100MB ) TO FILEGROUP ; Notice that we didn’t use CHAR(10), “+” Signs, or any single quotes in the body. In fact, no concatenation at all.

11 Encapsulate In REPLACEs and add some fnTally code to gen the dates…
+ REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(CAST(' RAISERROR("Working on FileGroup/File WITH NOWAIT; ALTER DATABASE ADD FILEGROUP ALTER DATABASE ADD FILE ( NAME = ,FILENAME = ,SIZE = 100MB ,FILEGROWTH = 100MB ) TO FILEGROUP ; ' ===== Replace the Tokens AS VARCHAR(MAX)) ,'"' ,'''') 112)) FROM t ORDER BY t.N OPTION (MAXDOP 1)

12 Add in the other things that you would normally have, and you’re done!
(See attached The Dynamic SQL.sql)

13 Quick Review Create a single iteration of “normal” code and test it.
Tokenize the places where variables or dynamic column data should go. Change single quotes to double quotes. Don’t use a CHAR(10)’s or “+” signs (concatenation) Encapsulate in a single pair of single quotes and a REPLACE for each token. Use like you normally would.

14 See me at the end of tonight’s presentations.
Q’n’A See me at the end of tonight’s presentations.

15 Dynamic SQL for the DBA by Jeff Moden
Heat Lightning Dynamic SQL for the DBA by Jeff Moden Thanks for Listening SPID Lightning Rounds, Detroit, Mi 12 September 2013


Download ppt "Dynamic SQL for the DBA by Jeff Moden"

Similar presentations


Ads by Google