Working with Long Strings by Jeff Moden Rainbow Lightning Working with Long Strings by Jeff Moden SPID Lightning Rounds, Detroit, Mi 12 September 2013
Your Speaker - Jeff Moden 17 years experience working with SQL Server Mostly Self Taught One of Leading Posters on SQLServerCentral.com More than 32,000 posts (heh… some are even useful) 30+ articles on the “Black Arts” of T-SQL http://www.sqlservercentral.com/Authors/Articles/Jeff_Moden/80567/ 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 ;-) Working with Long Strings 12 September 2013 © Copyright by Jeff Moden - All Rights Reserved
Agenda I call it “Rainbow Lightning” because it will allow us to see the “End of the Rainbow” where long strings are concerned. The Problem with Viewing Long Dynamic SQL An Easy Fix A Convenient Function Wrapping Long T-SQL Literals for Readability Working with Long Strings 12 September 2013 © Copyright by Jeff Moden - All Rights Reserved
The Problem with Viewing Long Dynamic SQL
The Display Problem To summarize the problem DEMOs Print works fine but is limited to something less than 8,000 characters (4,000 if using NVARCHAR) Display to the Grid removes all formatting and converts to a single line DEMOs (See attached files) Working with Long Strings 12 September 2013 © Copyright by Jeff Moden - All Rights Reserved
Sloshing
What is “Sloshing”? It’s the Reverse of Concatenation Allows splitting up of long strings for display/readability purposes Working with Long Strings 12 September 2013 © Copyright by Jeff Moden - All Rights Reserved
Examples Working with Long Strings --===== The command must appear as a single line to xp_CmdShell -- (There's a TAB character between the empty double quotes) -- but it doesn't fit the screen (indicated by the dashed lines) EXEC xp_cmdshell 'sqlcmd -E -S "VAIO" -h-1 -s" " -W -Q "SELECT * FROM master.sys.master_files" -o c:\temp\QueryResults.txt' ; -------------------------------------------------------------------------- --===== This is one way to fix the problem but it does take some doing... DECLARE @Cmd VARCHAR(8000); SELECT @Cmd = 'sqlcmd -E -S "VAIO" -h-1 -s" " -W -Q ' + '"SELECT * FROM master.sys.master_files" -o ' + 'c:\temp\QueryResults.txt' EXEC xp_cmdshell @Cmd --===== The command must appear as a single line to xp_CmdShell but -- we can "split" the line up a lot more easily with "Sloshing". -- A backslash followed by presing the enter key is call "SLOSHING" EXEC xp_cmdshell 'sqlcmd -E -S "VAIO" -h-1 -s" " -W -Q "SELECT * FROM mas\ ter.sys.master_files" -o c:\temp\QueryResults.txt' Working with Long Strings 12 September 2013 © Copyright by Jeff Moden - All Rights Reserved
Quick Review Learned quick an easy way to display ALL of the Dynamic SQL using an XML trick. Created a ShowBigString function to make it even easier. Learned how to “Slosh” (unconcatenate) long strings for readability without having to create a variable, etc. Working with Long Strings 12 September 2013 © Copyright by Jeff Moden - All Rights Reserved
See me at the end of tonight’s presentations. Q’n’A See me at the end of tonight’s presentations.
Working with Long Strings by Jeff Moden Rainbow Lightning Working with Long Strings by Jeff Moden Thanks for Listening SPID Lightning Rounds, Detroit, Mi 12 September 2013