tSQL Cursors and String functions
-
Upload
aaron-buma -
Category
Technology
-
view
748 -
download
3
Transcript of tSQL Cursors and String functions
![Page 1: tSQL Cursors and String functions](https://reader034.fdocuments.us/reader034/viewer/2022042821/55d59326bb61ebd45a8b4678/html5/thumbnails/1.jpg)
Cursors andString Functions
Presented by Steve Stedman and Aaron Buma
![Page 2: tSQL Cursors and String functions](https://reader034.fdocuments.us/reader034/viewer/2022042821/55d59326bb61ebd45a8b4678/html5/thumbnails/2.jpg)
WelcomeWelcome to all those joining us remotely.
For any questions via Google On Air Broadcasts, we will address most of these at the end of the training.
Training provided by Emergency Reportinghttp://EmergencyReporting.com
Slides and sample code are available at:http://SteveStedman.com
![Page 3: tSQL Cursors and String functions](https://reader034.fdocuments.us/reader034/viewer/2022042821/55d59326bb61ebd45a8b4678/html5/thumbnails/3.jpg)
Welcome Viewers FromBellingham, WAGlacier, WAWarren, MIChicago, ILHonolulu, HI
BelarusTrowbridge, UKChennai, IndiaPortugalMumbai, IndiaCape Town, South
AfricaBucharest
![Page 4: tSQL Cursors and String functions](https://reader034.fdocuments.us/reader034/viewer/2022042821/55d59326bb61ebd45a8b4678/html5/thumbnails/4.jpg)
Live BroadcastUsing Google On Air Broadcasts
There is about a 40 to 50 second delay from live to what you see.
We are still learning how to properly use Google On Air Broadcasts. Please be patient.
Session will be available on my YouTube Channel about an hour after the presentation ends. http://SteveStedman.com/YouTube
![Page 5: tSQL Cursors and String functions](https://reader034.fdocuments.us/reader034/viewer/2022042821/55d59326bb61ebd45a8b4678/html5/thumbnails/5.jpg)
QuestionsWe will have time for questions at the end of the
session.Q&A available via Google On Air Hangout panel.
Click the 3x3 grid icon near the top right, then select Q&A to see what people are asking, or to ask your own question.
When you ask a question, it shows up for us about 40 to 50 seconds delayed.
![Page 6: tSQL Cursors and String functions](https://reader034.fdocuments.us/reader034/viewer/2022042821/55d59326bb61ebd45a8b4678/html5/thumbnails/6.jpg)
Congratulations to AaronAaron took and passed the SQL Queries 70-
461 certification exam last Friday
As we go through the training, if others have taken and passed the certification exam based on our training, please let me know.
![Page 7: tSQL Cursors and String functions](https://reader034.fdocuments.us/reader034/viewer/2022042821/55d59326bb61ebd45a8b4678/html5/thumbnails/7.jpg)
AgendaCursorsString Functions
![Page 8: tSQL Cursors and String functions](https://reader034.fdocuments.us/reader034/viewer/2022042821/55d59326bb61ebd45a8b4678/html5/thumbnails/8.jpg)
Cursors
Presented by Steve Stedman
![Page 9: tSQL Cursors and String functions](https://reader034.fdocuments.us/reader034/viewer/2022042821/55d59326bb61ebd45a8b4678/html5/thumbnails/9.jpg)
CURSORsA method for iterating over a result set in
TSQLOne row at a time
![Page 10: tSQL Cursors and String functions](https://reader034.fdocuments.us/reader034/viewer/2022042821/55d59326bb61ebd45a8b4678/html5/thumbnails/10.jpg)
DECLARE @BusinessCursor as CURSOR;SET @BusinessCursor = CURSOR FORSELECT TOP 50 BusinessEntityID, Name FROM Sales.Store; OPEN @BusinessCursor;FETCH NEXT FROM @BusinessCursor INTO @BusinessEntityID, @BusinessName; WHILE @@FETCH_STATUS = 0BEGIN PRINT cast(@BusinessEntityID as VARCHAR (50)) + ' ' + @BusinessName; FETCH NEXT FROM @BusinessCursor INTO @BusinessEntityID, @BusinessName;END
CLOSE @BusinessCursor;DEALLOCATE @BusinessCursor;
![Page 11: tSQL Cursors and String functions](https://reader034.fdocuments.us/reader034/viewer/2022042821/55d59326bb61ebd45a8b4678/html5/thumbnails/11.jpg)
sp_describe_cursorGet detailed information about your cursor.
![Page 12: tSQL Cursors and String functions](https://reader034.fdocuments.us/reader034/viewer/2022042821/55d59326bb61ebd45a8b4678/html5/thumbnails/12.jpg)
FORWARD_ONLY CursorFrom Books Online:
FORWARD_ONLY Specifies that the cursor can only be scrolled from the first to the last row. FETCH NEXT is the only supported fetch option.
Faster than other options
![Page 13: tSQL Cursors and String functions](https://reader034.fdocuments.us/reader034/viewer/2022042821/55d59326bb61ebd45a8b4678/html5/thumbnails/13.jpg)
FAST_FORWARDAlso a forward only cursorUsually the fastest option
![Page 14: tSQL Cursors and String functions](https://reader034.fdocuments.us/reader034/viewer/2022042821/55d59326bb61ebd45a8b4678/html5/thumbnails/14.jpg)
SCROLL CursorFrom Books Online:
Specifies that all fetch options (FIRST, LAST, PRIOR, NEXT, RELATIVE, ABSOLUTE) are available.
![Page 15: tSQL Cursors and String functions](https://reader034.fdocuments.us/reader034/viewer/2022042821/55d59326bb61ebd45a8b4678/html5/thumbnails/15.jpg)
Performance IssuesCursors are generally not as performant as a
set logic way of approaching a solution.There is almost always a better way of doing
things than using a cursor.Nested cursors become an even bigger
performance and blocking issue.
![Page 16: tSQL Cursors and String functions](https://reader034.fdocuments.us/reader034/viewer/2022042821/55d59326bb61ebd45a8b4678/html5/thumbnails/16.jpg)
RBAR – Row by Agonizing RowTake a system (SQL Server) that is designed
to processes massive sets of data with JOINs and other logic.
Throw out all those set based performance benefits, and look at one row at a time in a result set.
![Page 17: tSQL Cursors and String functions](https://reader034.fdocuments.us/reader034/viewer/2022042821/55d59326bb61ebd45a8b4678/html5/thumbnails/17.jpg)
Time and PlaceThere is a time and place for CURSORs, it is just
that those times and places are few and far between.
Example: Calculating Prime Numbers. Worked great with set logic for about the first 100,000,000 prime numbers, but became very slow after that. I use a cursor to process batches at a time. Currently at 753,000,000 prime numbers using a cursor.
![Page 18: tSQL Cursors and String functions](https://reader034.fdocuments.us/reader034/viewer/2022042821/55d59326bb61ebd45a8b4678/html5/thumbnails/18.jpg)
CURSORS
Demo
![Page 19: tSQL Cursors and String functions](https://reader034.fdocuments.us/reader034/viewer/2022042821/55d59326bb61ebd45a8b4678/html5/thumbnails/19.jpg)
String Functions
Presented by Aaron Buma
![Page 20: tSQL Cursors and String functions](https://reader034.fdocuments.us/reader034/viewer/2022042821/55d59326bb61ebd45a8b4678/html5/thumbnails/20.jpg)
String FunctionsLEN() – Returns length: SELECT
LEN(columnA) AS …
![Page 21: tSQL Cursors and String functions](https://reader034.fdocuments.us/reader034/viewer/2022042821/55d59326bb61ebd45a8b4678/html5/thumbnails/21.jpg)
String FunctionsSections of string:
LEFT(), RIGHT() and SUBSTRING()
LEFT/RIGHT (columnA, DistanceFromEdge) AS …
SUBSTRING (columnA, StartPosition, Distance) AS … StartPosition can be any number, 0 or 1 is the start
character
![Page 22: tSQL Cursors and String functions](https://reader034.fdocuments.us/reader034/viewer/2022042821/55d59326bb61ebd45a8b4678/html5/thumbnails/22.jpg)
String FunctionsChanging Strings
LTRIM, RTRIM: Removes leading empty spaces from a side
UPPER, LOWER: Forces upper or lower case
REPLACE: Replaces an defined set of characters with another
![Page 23: tSQL Cursors and String functions](https://reader034.fdocuments.us/reader034/viewer/2022042821/55d59326bb61ebd45a8b4678/html5/thumbnails/23.jpg)
String Functions – 2012 Intro’sCONCAT
Makes field concatenation cleaner and easierFORMAT
A simplified way to format Dates (localization too!) Currency Time Numbers (with custom formatting)
![Page 24: tSQL Cursors and String functions](https://reader034.fdocuments.us/reader034/viewer/2022042821/55d59326bb61ebd45a8b4678/html5/thumbnails/24.jpg)
Bonus – Table and Field MetadataSERVERPROPERTY()
Usable for server info
OBJECTPROPERTY()Easy way to get object propertiesISMSSHIPPED – Did it come with SQL server?
![Page 25: tSQL Cursors and String functions](https://reader034.fdocuments.us/reader034/viewer/2022042821/55d59326bb61ebd45a8b4678/html5/thumbnails/25.jpg)
String Functions
Demo
![Page 26: tSQL Cursors and String functions](https://reader034.fdocuments.us/reader034/viewer/2022042821/55d59326bb61ebd45a8b4678/html5/thumbnails/26.jpg)
Any Questions?CursorsString Functions
![Page 27: tSQL Cursors and String functions](https://reader034.fdocuments.us/reader034/viewer/2022042821/55d59326bb61ebd45a8b4678/html5/thumbnails/27.jpg)
Tune in next weekThursday 3/19 at 9:00am pacific time
Time FunctionsLogical FunctionsUser Defined Functions
![Page 28: tSQL Cursors and String functions](https://reader034.fdocuments.us/reader034/viewer/2022042821/55d59326bb61ebd45a8b4678/html5/thumbnails/28.jpg)
For More InformationVisit http://EmergencyReporting.com to find
out more about Emergency Reporting.Aaron on the web
http://AaronBuma.comTwitter: @AaronDBuma
Steve on the webhttp://SteveStedman.comtwitter: @SqlEmt