13
SQL Spackle #1 SQL Spackle #1 Jeff Moden Jeff Moden 19 May 2011 19 May 2011

SQL Spackle #1 Jeff Moden 19 May 2011. About Your Speaker Mostly Self Trained Started with SQL Server in 1995 More than 25,000 posts on SQLServerCentral.com

Embed Size (px)

Citation preview

Page 1: SQL Spackle #1 Jeff Moden 19 May 2011. About Your Speaker Mostly Self Trained Started with SQL Server in 1995 More than 25,000 posts on SQLServerCentral.com

SQL Spackle #1SQL Spackle #1

Jeff ModenJeff Moden

19 May 201119 May 2011

Page 2: SQL Spackle #1 Jeff Moden 19 May 2011. About Your Speaker Mostly Self Trained Started with SQL Server in 1995 More than 25,000 posts on SQLServerCentral.com

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

Page 3: SQL Spackle #1 Jeff Moden 19 May 2011. About Your Speaker Mostly Self Trained Started with SQL Server in 1995 More than 25,000 posts on SQLServerCentral.com

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

Page 4: SQL Spackle #1 Jeff Moden 19 May 2011. About Your Speaker Mostly Self Trained Started with SQL Server in 1995 More than 25,000 posts on SQLServerCentral.com

Introduction Introduction ToTo

“Pseudo Cursors”“Pseudo Cursors”

Page 5: SQL Spackle #1 Jeff Moden 19 May 2011. About Your Speaker Mostly Self Trained Started with SQL Server in 1995 More than 25,000 posts on SQLServerCentral.com

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.

Page 6: SQL Spackle #1 Jeff Moden 19 May 2011. About Your Speaker Mostly Self Trained Started with SQL Server in 1995 More than 25,000 posts on SQLServerCentral.com

““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.

Page 7: SQL Spackle #1 Jeff Moden 19 May 2011. About Your Speaker Mostly Self Trained Started with SQL Server in 1995 More than 25,000 posts on SQLServerCentral.com

““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

Page 8: SQL Spackle #1 Jeff Moden 19 May 2011. About Your Speaker Mostly Self Trained Started with SQL Server in 1995 More than 25,000 posts on SQLServerCentral.com

““Pseudo Cursor”Pseudo Cursor”ExamplesExamples

Page 9: SQL Spackle #1 Jeff Moden 19 May 2011. About Your Speaker Mostly Self Trained Started with SQL Server in 1995 More than 25,000 posts on SQLServerCentral.com

TheThe“Calendar” Table“Calendar” Table

Page 10: SQL Spackle #1 Jeff Moden 19 May 2011. About Your Speaker Mostly Self Trained Started with SQL Server in 1995 More than 25,000 posts on SQLServerCentral.com

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.

Page 11: SQL Spackle #1 Jeff Moden 19 May 2011. About Your Speaker Mostly Self Trained Started with SQL Server in 1995 More than 25,000 posts on SQLServerCentral.com

Building theBuilding the“Calendar” Table“Calendar” Table

Page 12: SQL Spackle #1 Jeff Moden 19 May 2011. About Your Speaker Mostly Self Trained Started with SQL Server in 1995 More than 25,000 posts on SQLServerCentral.com

““Calendar” TableCalendar” TableCode ExamplesCode Examples

Page 13: SQL Spackle #1 Jeff Moden 19 May 2011. About Your Speaker Mostly Self Trained Started with SQL Server in 1995 More than 25,000 posts on SQLServerCentral.com

GroupingGrouping “Islands of Dates” “Islands of Dates”