Upload
caren-dorsey
View
213
Download
0
Embed Size (px)
Citation preview
SQL Spackle #1SQL Spackle #1
Jeff ModenJeff Moden
19 May 201119 May 2011
About Your SpeakerAbout Your SpeakerMostly Self TrainedMostly Self TrainedStarted with SQL Server in 1995Started with SQL Server in 1995More than 25,000 posts on SQLServerCentral.com More than 25,000 posts on SQLServerCentral.com (some are actually useful)(some are actually useful)
22 Articles on SQLServerCentral.com22 Articles on SQLServerCentral.com– http://www.sqlservercentral.com/Authors/Articles/http://www.sqlservercentral.com/Authors/Articles/
Jeff_Moden/80567/Jeff_Moden/80567/
SQL Server MVP since 2008SQL Server MVP since 2008Coined the phrase “RBAR”Coined the phrase “RBAR”2 Sessions at PASS 20102 Sessions at PASS 2010SQL Server Saturday #60 – Brunswick, OHSQL Server Saturday #60 – Brunswick, OH
AgendaAgendaIntroduction to “Pseudo Cursors”Introduction to “Pseudo Cursors”– What they areWhat they are– Declarative CodeDeclarative Code– Procedural CodeProcedural Code– ExamplesExamples
The Calendar TableThe Calendar Table– What it isWhat it is– How to build itHow to build it– Usage examplesUsage examples
Introduction Introduction ToTo
“Pseudo Cursors”“Pseudo Cursors”
Behind the Scenes…Behind the Scenes…What IS SQL Server What IS SQL Server ReallyReally??– It’s nothing more than a very sophisticated file It’s nothing more than a very sophisticated file
system.system.
What IS a SELECT?What IS a SELECT?– It’s nothing more than “It’s nothing more than “declarativedeclarative” code that ” code that
loops through a part of a file identified as a loops through a part of a file identified as a “table”.“table”.
– It does almost like the code you’d write… find a It does almost like the code you’d write… find a row, read a row, process a row, loop until done.row, read a row, process a row, loop until done.
– It LOOPs in the background so you don’t have It LOOPs in the background so you don’t have to write loops like a front-end Developer.to write loops like a front-end Developer.
““Declarative” CodeDeclarative” Code
You tell it what you want doneYou tell it what you want done
SQL Server figures out how to do it.SQL Server figures out how to do it.
““Set Based” code is where you allow Set Based” code is where you allow or even help SQL Server do the best or even help SQL Server do the best job that it can do.job that it can do.
It works with more than one row at a It works with more than one row at a time or in “sets” of rows.time or in “sets” of rows.
““Procedural” CodeProcedural” CodeEasy name to remember because Easy name to remember because you tell SQL Server how to “proceed” you tell SQL Server how to “proceed” every step of the way.every step of the way.Consists of explicit LoopsConsists of explicit LoopsConsists of “hidden” RBAR such as Consists of “hidden” RBAR such as recursive CTE’s and recursive recursive CTE’s and recursive functions.functions.Is usually quite slow because SQL Is usually quite slow because SQL Server can’t do what it’s supposed to Server can’t do what it’s supposed to
““Pseudo Cursor”Pseudo Cursor”ExamplesExamples
TheThe“Calendar” Table“Calendar” Table
What A Calendar Table IsWhat A Calendar Table Is
A “Helper” TableA “Helper” Table
Auxiliary Table of DatesAuxiliary Table of Dates
A special kind of “Pseudo Cursor”A special kind of “Pseudo Cursor”
Pre-populated with date related infoPre-populated with date related info
Use to replace Cursors, While loops, Use to replace Cursors, While loops, and other complicated code.and other complicated code.
Building theBuilding the“Calendar” Table“Calendar” Table
““Calendar” TableCalendar” TableCode ExamplesCode Examples
GroupingGrouping “Islands of Dates” “Islands of Dates”