Tally Function with Error Checking by Jeff Moden

Slides:



Advertisements
Similar presentations
Lecture-5 Though SQL is the natural language of the DBA, it suffers from various inherent disadvantages, when used as a conventional programming language.
Advertisements

Overloading methods review When is the return statement required? What do the following method headers tell us? public static int max (int a, int b)
Chapter 2: Input, Processing, and Output
Copyright © 2010 Pearson Education, Inc. All rights reserved Sec Set Operations and Compound Inequalities.
Chapter 7 Advanced SQL Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
SQL Spackle #1 Jeff Moden 19 May About Your Speaker Mostly Self Trained Started with SQL Server in 1995 More than 25,000 posts on SQLServerCentral.com.
Analysing Indexes SQLBits 6 th October 2007 © Colin Leversuch-Roberts Kelem Consulting Limited September 2007.
Said Salomon Unitrin Direct Insurance T-SQL Avoiding cursors Said Salomon.
1 11g NEW FEATURES ByVIJAY. 2 AGENDA  RESULT CACHE  INVISIBLE INDEXES  READ ONLY TABLES  DDL WAIT OPTION  ADDING COLUMN TO A TABLE WITH DEFAULT VALUE.
What Is The SSIS Catalog and Why Do I Care?
The Tally Table and Pseudo Cursors
CSC201: Computer Programming
Chapter 2: Input, Processing, and Output
Chapter 2, Part I Introduction to C Programming
T-SQL Coding Techniques Are you playing with fire?
SQL Saturday Pittsburgh
The "Numbers" or "Tally" Table:
CS1010 Discussion Group 11 Week 6 – One dimensional arrays.
Chapter Topics 2.1 Designing a Program 2.2 Output, Input, and Variables 2.3 Variable Assignment and Calculations 2.4 Variable Declarations and Data Types.
Error Handling Summary of the next few pages: Error Handling Cursors.
Simplifying XEvents Management with dbatools
DevOps Database Administration
SQL Server May Let You Do It, But it Doesn’t Mean You Should
Introduction to Execution Plans
Query Execution Expectation-Reality Denis Reznik
Recommended Budget Reductions
Using the Set Operators
The Ins and Outs of Indexes
Agenda Database Development – Best Practices Why Performance Matters ?
TSQL Coding Techniques
DevOps Database Administration
Dynamic SQL for the DBA by Jeff Moden
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Advanced PL/SQL Programing
Query Optimization Techniques
INPUT & OUTPUT scanf & printf.
Azure SQL DWH: Tips and Tricks for developers
Hidden gems of SQL Server 2016
Escape sequences: Practice using the escape sequences on the code below to see what happens. Try this next code to help you understand the last two sequences.
Row Level Security in SQL Azure and in On Premise
Tally Ho! -- Explore the Varied Uses of Tally Tables
Working with Long Strings by Jeff Moden
Lial/Hungerford/Holcomb/Mullins: Mathematics with Applications 11e Finite Mathematics with Applications 11e Copyright ©2015 Pearson Education, Inc. All.
Ensuring a Remote Server is Available
IPC144 Introduction to Programming Using C Week 8 – Lesson 1
String What it is Why it’s useful
The Hidden Mysteries of Common Table Expressions
3.1 Iteration Loops For … To … Next 18/01/2019.
Nested Loops & The Step Parameter
Moving from SQL Profiler to xEvents
Azure SQL DWH: Tips and Tricks for developers
Anatomy of a Java Program
Introduction to Execution Plans
Chapter 8 Advanced SQL.
The Ins and Outs of Indexes
Azure SQL DWH: Tips and Tricks for developers
Diving into Query Execution Plans
IPC144 Introduction to Programming Using C Week 4 – Lesson 2
Chapter 2: Input, Processing, and Output
Introduction to Execution Plans
Query Optimization Techniques
Introduction to Execution Plans
Why should I care about SQL, if I have ORM?
Reinhard Flügel Possiblities and Limitations of System-Versioned Temporal Tables beyond the Basics.
Reinhard Flügel Possiblities and Limitations of System-Versioned Temporal Tables beyond the Basics.
Reinhard Flügel Possiblities and Limitations of System-Versioned Temporal Tables beyond the Basics.
Improving the Performance of Functions
THANK YOU for helping make tonight possible
Handling Data Errors in a Dataflow Task
Presentation transcript:

Tally Function with Error Checking by Jeff Moden Greased Lightning Tally Function with Error Checking 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 33,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 ;-) Tally Function with Error Checking 12 September 2013 © Copyright by Jeff Moden - All Rights Reserved

Agenda The Tally Function Error Checking in a Function A high performance “readless” replacement for the Tally Table Error Checking in a Function How to check for and display meaningful errors from a function Quick Demo of Performance and Error Messages Tally Function with Error Checking 12 September 2013 © Copyright by Jeff Moden - All Rights Reserved

The Tally Function

Reference Material Learn What a Tally Table/Function is and How It Replaces Certain Types of Loops: http://www.sqlservercentral.com/articles/T-SQL/62867/ How to Split Strings: http://www.sqlservercentral.com/articles/Tally+Table/72993/

The Tally Function Tally Function with Error Checking CREATE FUNCTION dbo.fnTally --===== Define the I/O for this function (@BaseValue INT, @MaxValue INT) RETURNS TABLE WITH SCHEMABINDING AS RETURN WITH --===== Generate up to 1 Million rows ("En" indicates the power of 10 produced) --E1(N) AS (SELECT 1 FROM (VALUES (1),(1),(1),(1),(1),(1),(1),(1),(1),(1))n(N)), E1(N) AS (SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1), E3(N) AS (SELECT 1 FROM E1 a,E1 b,E1 c), E6(N) AS (SELECT 1 FROM E3 a,E3 b) --===== Conditionally start the sequence at 0 SELECT N = 0 WHERE @BaseValue = 0 UNION ALL --===== Enumerate the rows generated by the cascading CTEs (cCTE) SELECT TOP (@MaxValue) N = CAST(ROW_NUMBER()OVER(ORDER BY (SELECT N)) AS INT) FROM E6 ; Explain why this is an “Inline” function and why that’s important for performance. Explain how the “cascading CTE’s” work as a “row source”/”pseudo-cursor”. Explain the difference between the commented out VALUES statement and E1. Explain how the combination of ROW_NUMBER() and TOP produce values from 1 to @MaxValue. Explain how the SELECT N=0 WHERE @BaseValue=0 works to start a sequence at 0 or 1. Tally Function with Error Checking 12 September 2013 © Copyright by Jeff Moden - All Rights Reserved

Error Checking in a Function

Error Checking in a Function CREATE FUNCTION dbo.fnTally --===== Define the I/O for this function (@BaseValue INT, @MaxValue INT) RETURNS TABLE WITH SCHEMABINDING AS RETURN WITH --===== Generate up to 1 Million rows ("En" indicates the power of 10 produced) --E1(N) AS (SELECT 1 FROM (VALUES (1),(1),(1),(1),(1),(1),(1),(1),(1),(1))n(N)), E1(N) AS (SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1), E3(N) AS (SELECT 1 FROM E1 a,E1 b,E1 c), E6(N) AS (SELECT 1 FROM E3 a,E3 b) --===== Create understandable error messages if the parameters are incorrect. SELECT N = CAST('Util.dbo.fnTally: @BaseValue must be 0 or 1' AS INT) WHERE ISNULL(@BaseValue,-1) NOT BETWEEN 0 AND 1 UNION ALL SELECT N = CAST('Util.dbo.fnTally: @MaxValue must be between 0 and 1 Million' AS INT) WHERE @MaxValue > 1000000 --Negative values taken care of by TOP error --===== Conditionally start the sequence at 0 SELECT N = 0 WHERE @BaseValue = 0 --===== Enumerate the rows generated by the cascading CTEs (cCTE) SELECT TOP (@MaxValue) N = CAST(ROW_NUMBER()OVER(ORDER BY (SELECT N)) AS INT) FROM E6 ; Explain that this is identical to the previous code (the function) with some added room. Explain that RAISERROR and tradition error checking methods cannot be used. When the error checking code comes on screen, explain the error checking message and conditions for each error check. Tally Function with Error Checking 12 September 2013 © Copyright by Jeff Moden - All Rights Reserved

(See 01 fnTally with Error Checking.sql) Quick Demo (See 01 fnTally with Error Checking.sql)

Quick Review The Tally Function Error Checking in a Function A high performance “readless” replacement for the Tally Table Error Checking in a Function How to check for and display meaningful errors from a function Quick Demo of Performance and Error Messages It’s real fast and the error messages help, but a real Tally Table is still faster. Tally Function with Error Checking 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. Tally Function with Error Checking 12 September 2013 © Copyright by Jeff Moden - All Rights Reserved

Tally Function with Error Checking by Jeff Moden Greased Lightning Tally Function with Error Checking by Jeff Moden Thanks for Listening SPID Lightning Rounds, Detroit, Mi 12 September 2013