DAT430 Extending SQL Server 2000 Functionality with User-Defined Functions: Hidden Tricks Fernando G. Guerrero S olid Q uality L earning

Slides:



Advertisements
Similar presentations
Access 2007 ® Use Databases How can Microsoft Access 2007 help you structure your database?
Advertisements

Advantage Data Dictionary. agenda Creating and Managing Data Dictionaries –Tables, Indexes, Fields, and Triggers –Defining Referential Integrity –Defining.
Brian Alderman | MCT, CEO / Founder of MicroTechPoint Pete Harris | Microsoft Senior Content Publisher.
DEV392: Extending SharePoint Products And Technologies Through Web Parts And ASP.NET Clint Covington, Program Manager Data And Developer Services - Office.
Instructor: Craig Duckett CASE, ORDER BY, GROUP BY, HAVING, Subqueries
Brian Alderman | MCT, CEO / Founder of MicroTechPoint Pete Harris | Microsoft Senior Content Publisher.
Chapter 7 Managing Data Sources. ASP.NET 2.0, Third Edition2.
Architecting a Large-Scale Data Warehouse with SQL Server 2005 Mark Morton Senior Technical Consultant IT Training Solutions DAT313.
Denny Cherry twitter.com/mrdenny.
Introduction To Databases IDIA 618 Fall 2014 Bridget M. Blodgett.
DAT304 Leveraging XML and HTTP with Sql Server Irwin Dolobowsky Program Manager Webdata Group.
DAY 21: MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Akhila Kondai October 30, 2013.
DAT308 SQL Server 2000 Profiler: The Database Programmer's Best Friend Fernando G. Guerrero S olid Q uality L earning
OFC302 Building Smart Document Solutions in Word & Excel Martin Sawicki Lead Program Manager.
Using XML in SQL Server 2005 NameTitleCompany. XML Overview Business Opportunity The majority of all data transmitted electronically between organizations.
Introduction to Databases Chapter 6: Understanding the SQL Language.
Functions Lesson 10. Skills Matrix Function A function is a piece of code or routine that accepts parameters and stored as an object in SQL Server. The.
Developing Workflows with SharePoint Designer David Coe Application Development Consultant Microsoft Corporation.
DAT305 Boost Your Data-Driven Application Development Using SQL Server Centric.NET Code Generator Pascal Belaud Microsoft France.
Module 1: Introduction to Transact-SQL
DEV325 Deploying Visual Studio.NET Applications Billy Hollis Author / Consultant.
SQL/Lesson 4/Slide 1 of 45 Using Subqueries and Managing Databases Objectives In this lesson, you will learn to: *Use subqueries * Use subqueries with.
1 Definition of a subquery Nested subqueries Correlated subqueries The ISNULL function Derived tables The EXISTS operator Mixing data types: CAST & CONVERT.
Module 11: Programming Across Multiple Servers. Overview Introducing Distributed Queries Setting Up a Linked Server Environment Working with Linked Servers.
PowerBuilder Online Courses - by Prasad Bodepudi
5/24/01 Leveraging SQL Server 2000 in ColdFusion Applications December 9, 2003 Chris Lomvardias SRA International
CS 101 – Access notes Databases (Microsoft Access) 4 parts of a database database design –Try to understand the ideas behind database design, not just.
Triggers A Quick Reference and Summary BIT 275. Triggers SQL code permits you to access only one table for an INSERT, UPDATE, or DELETE statement. The.
Table Indexing for the.NET Developer Denny Cherry twitter.com/mrdenny.
Views Lesson 7.
INFO1408 Database Design Concepts Week 15: Introduction to Database Management Systems.
Access 2007 ® Use Databases How can Microsoft Access 2007 help you structure your database?
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Denny Cherry twitter.com/mrdenny.
DBA 328 Designing for Performance: Optimization with Indexes Kimberly L. Tripp Solid Quality Learning – SolidQualityLearning.com
Constraints cis 407 Types of Constraints & Naming Key Constraints Unique Constraints Check Constraints Default Constraints Misc Rules and Defaults Triggers.
SQL Server 2005 Implementation and Maintenance Chapter 3: Tables and Views.
06 | Modifying Data in SQL Server Brian Alderman | MCT, CEO / Founder of MicroTechPoint Tobias Ternstrom | Microsoft SQL Server Program Manager.
DAT 332 SQL Server 2000 Data Transformation Services (DTS) Best Practices Euan Garden Product Unit Manager SQL Server Development Microsoft Corporation.
DEV330 Visual Studio.NET IDE Tips and Tricks Billy Hollis Author / consultant.
IMS 4212: Data Manipulation 1 Dr. Lawrence West, MIS Dept., University of Central Florida Additional Data Manipulation Statements INSERT.
SQL SERVER DAYS 2011 Table Indexing for the.NET Developer Denny Cherry twitter.com/mrdenny.
MSG331 Exchange Server 2000/2003 Software Development Kit Susan Hill Lead Programmer Writer Microsoft Corporation.
DAT300 SQL Server Notification Services: Application Development Ken Henderson Technical Lead, SQL Server Support Microsoft Corporation
Session 1 Module 1: Introduction to Data Integrity
Learningcomputer.com SQL Server 2008 –Views, Functions and Stored Procedures.
05 | SET Operators, Windows Functions, and Grouping Brian Alderman | MCT, CEO / Founder of MicroTechPoint Tobias Ternstrom | Microsoft SQL Server Program.
Module 3: Using XML. Overview Retrieving XML by Using FOR XML Shredding XML by Using OPENXML Introducing XQuery Using the xml Data Type.
Ch 5. Introducing More Database Objects. Database Objects Table (ch2) View (ch3) Stored Procedure Trigger Function User-defined types.
Pinal Dave Mentor | Solid Quality India |
IMS 4212: Application Architecture and Intro to Stored Procedures 1 Dr. Lawrence West, Management Dept., University of Central Florida
DAT468 Extending SQL Server 2000 Functionality with User-Defined Functions: Hidden Tricks Fernando G. Guerrero
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
SQL Triggers, Functions & Stored Procedures Programming Operations.
Diving into Query Execution Plans ED POLLACK AUTOTASK CORPORATION DATABASE OPTIMIZATION ENGINEER.
Module 9: Implementing Functions. Overview Creating and Using Functions Working with Functions Controlling Execution Context.
DAT 390 Advanced ADO.NET Programming Techniques Jackie Goldstein Renaissance Computer Systems
Data Integrity & Indexes / Session 1/ 1 of 37 Session 1 Module 1: Introduction to Data Integrity Module 2: Introduction to Indexes.
SQL IMPLEMENTATION & ADMINISTRATION Indexing & Views.
Understanding Core Database Concepts Lesson 1. Objectives.
6/16/2018 © 2014 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks.
PROCEDURES, CONDITIONAL LOGIC, EXCEPTION HANDLING, TRIGGERS
Optimizing Microsoft SQL Server 2008 Applications Using Table Valued Parameters, XML, and MERGE
Principles of report writing
ISC440: Web Programming 2 Server-side Scripting PHP 3
TechEd /15/2019 8:08 PM © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks.
Relational Database Design
Why You Should Consider Implementing Indexed Views
Designing and Implementing User- Defined Functions
Presentation transcript:

DAT430 Extending SQL Server 2000 Functionality with User-Defined Functions: Hidden Tricks Fernando G. Guerrero S olid Q uality L earning

Perhaps you heard about UDFs somewhere Perhaps you know how to convert stored procedures into UDFs Or to created parameterized views (called now Inline UDFs) Or to extend SQL Server builtin function with your own scalar UDFs Is there anything else you could do with them?

Quick info about Fernando (2 milliseconds) MCSD, MCSE+Internet (W2K), MCDBA, MCT, SQL Server MVP CEO and Principal Mentor at Solid Quality Learning Writing for SQL Sever Magazine and SQL Server Professional Co-author of Microsoft SQL Server 2000 Programming by Example (ISBN : ) Author of the.NET Operations Guide and the.NET Deployment Guide for Microsoft TechNet

S olid Q uality L earning (3 ms) An association of SQL Server experts from around the world Principal Mentors: Itzik Ben-Gan Kalen Delaney Fernando G. Guerrero Michael Hotek Brian Moran Ron Talmage Kimberly L. Tripp Helping you get the best out of your SQL Server: Training Consulting Mentoring Stay tuned to

Agenda Bypassing UDFs limitations Some undocumented UDFs you should know about How to create a system UDF How to redefine system stored procedures as system UDFs Combining UDFs with Indexed views Indexing computed columns based on UDFs Modifying data through UDFs

A little hidden UDF gem: fn_dblog demo demo

Bypassing UDFs Limitations When a user defined UDF is deterministic? Can we build non-deterministic UDFs? Can use non-deterministic functions inside a UDF?

When is a user-defined UDF deterministic? Every function, built-in or user-defined, referenced in the function is deterministic. The function is defined with the SCHEMABINDING option. The function does not execute extended stored procedures. You can use built-in non-deterministic functions inside a UDF, even if BOL tells you that it is not possibleUDFBOL

Using non-deterministic functions inside a UDF If a function is not valid inside a UDF and you really need to use it there: Use a RowSet function: OPENROWSET OPENQUERY OPENDATASOURCE Examples: Generate random numbers from a UDF Using Date functions inside a UDF

Limitations’ workarounds and some tricks for Scalar UDF How to get random numbers from a UDF How to use dates inside UDF Recursion in scalar UDF How to modify actual data from inside a UDF Let’s look at them in more detail!

How to get random numbers from a UDF RAND is not allowed inside a UDF Get time information from sysprocesses (last_batch) Only meaningful for user connections Returns service start time for system processes (such as Lazy Writer) Not very good approach Make it more complex combining some trig or log functions (such as PI) Use OPENQUERY to call back and everything will be allowed…… including RAND !?!?!?!

How to use dates inside UDF Date related functions are not allowed inside a UDF Get time information from sysprocesses (last_batch), as before, but it isn’t reliable Use OPENQUERY or OPENROWSET to call back and everything will be allowed… including GetDate !?!?!?! including GetDate !?!?!?! Why date functions are not allowed inside UDFs? Perhaps someone at MS hates them

Recursion in scalar UDF Nothing wrong with it but… I would try a different solution Limited to 32 levels Could be useful to navigate binary trees A valid alternative to recursive stored procedure that retrieve data Example: The employees tree The employees tree

How to modify actual data from inside a UDF It is not allowed… by definition But if you can use ADO objects from VB to connect to SQL Server and modify data… You can do the same thing from UDFs with sp_OAxxx procedures However… I don’t see the need for it: I would always use stored procedures to modify SQL Server data But it is possible, as in this example, as in this example

Inserting with OPENQUERY? OPENQUERY is designed to retrieve data from any OLEDB Provider But it actually execute batches of instructions OPENQUERY makes and OLEDB connection back to SQL Server Let’s see what happens what happens

Can you “execute” a UDF? UDF style SELECT dbo.TotalPrice(12, 0.2, 25.4) Stored procedure style = TotalPrice = = 25.4 Some code Great for dealing with optional parameters, but you lose the entire reason of using a UDF

Using table variables inside UDFs Valid inside Scalar and Table valued UDFs This is the only case where you can use DML functions inside a UDF Not valid for Inline UDFs I’ll show you an exampleexample

Consuming XML data inside UDFs Consuming XML data inside a UDF requires execution of: sp_xml_preparedocument sp_xml_removedocument The CREATE UDF works very well But it can’t be used: Server: Msg 557, Level 16, State 2, Procedure GetProductsFromXML, Line 12 Only functions and extended stored procedures can be executed from within a function. I have an exampleexample

Returning XML data from UDFs The FOR XML clause is not valid inside a UDF However, you might think about using OPENQUERY: SELECT * FROM OPENQUERY(CLOCKWORK, 'SELECT TOP 5 * FROM Northwind.dbo.Products FOR XML AUTO') This query returns an image column, and this cannot be used as result of a scalar UDF The result of a scalar UDF is nothing else than a special OUTPUT parameter of the query plan Yes, I have an exampleexample

Some undocumented Scalar UDFs Where to find them? $SQLInstallDir$\Install\Replsys.sql $SQLInstallDir$\Install\Procsyst.sql Some examples on how to use themSome examples on how to use them: fn_chariswhitespace fn_replquotename fn_replmakestringliteral fn_mssharedversion Mostly defined for internal use by replication procedures I didn’t find any real use of them, but some definitions are very interesting

A Documented System Inline Returns trace file information in a table is the trace physical file is the number of rollover files to read (Default to all files) SELECT * FROM ::fn_trace_gettable( 'c:\my_trace.trc', default)

And an Undocumented System Inline Reads the Transaction is the starting Log Sequence Number is the last LSN Use (NULL, NULL) to read the entire log Be careful with format!!!! Convert them first from HEX:HEX:HEX into DEC:DEC:DEC SELECT TOP 10 [Current LSN], Operation FROM ::fn_dblog(NULL, NULL) ORDER BY [Current LSN] DESC

Joining UDFs UDF to UDF SELECT CompanyName, OrderID, TotalValue FROM dbo.OrdersByValue(10000) AS OBV JOIN dbo.CustomersByCountry(‘USA’) C ON OBV.CustomerID = C.CustomerID UDF to Table SELECT CompanyName, OrderID, TotalValue FROM dbo.OrdersByValue(10000) AS OBV JOIN Customers C ON OBV.CustomerID = C.CustomerID Some codecode

Creating system UDFs Must be created in the master database SQL Server must be configured to allow updates to system tables The owner must be system_function_schema That’s it! Very useful for converting system stored procedures into system UDFs: If you’d like to use a system procedure in the FROM clause And the procedure contains valid UDF code

Example: Converting sp_lock into fn_lock fn_lock can provide you better functionality than sp_lock: Search for waiting processes Search for locks affecting a given object Display locks affecting a given connection Display locks affecting the same resource Look at the code For example: SELECT * FROM ::fn_lock() WHERE Status = 'WAIT‘ You can convert sp_who into fn_who as well

Example: Getting Windows users and groups information fn_getntgroupmembers gives you the members of a Windows group fn_getntgroups gives you the groups that a given user belong too Look at the code For example: SELECT * FROM ::fn_getntgroups(‘MyDomain\MyUser')

UDFs in constraint definitions Using scalar UDFs as a DEFAULT definitions Using scalar UDFs in CHECK constraints Using scalar UDFs in PRIMARY KEY fields Using scalar UDFs in computed columns “UDFs Provide a New Identity” (SQL Server Magazine, March 2001): Look at the code: ALTER TABLE IDTest ADD CONSTRAINT def_IDTest DEFAULT (dbo.fn_GetNewID(OBJECT_ID('IDTest'))) FOR ID

Combining UDFs with Indexed views Create the view first, as complex as you’d need Create an index on the view by using the searchable column Create an InLine UDF selecting data from this view Use the searchable column as parameter of the UDF This provides efficient way of using this indexed view Look at this exampleexample

Indexing computed columns based on UDFs Create a computed column based on: A UDF A combination of UDFs The result needs to be precise and deterministic Create an index on this column in the usual way Only useful when: the UDF is complex to run and its result only changes from time to time Many reading operations using this expression as a searchable argument Look at this exampleexample

Modifying data through UDFs Only valid for InLine UDFs They are almost views, so they give you similar functionality Can you modify actual data through views? As long as the view is not read-only Or you define an INSTEAD of trigger for it Can you create INSTEAD OF triggers on a UDF? NO But you can create a UDF on top of a view that has INSTEAD OF triggers defined 

Inserting actual data through UDFs INSERT dbo.GetCustomersFromUSA() (CustomerID, CompanyName, Country) VALUES ('ZZZZZ', 'Dummy Customer', 'USA') Let’s take a look at the query planquery plan

Deleting actual data through UDFs DELETE dbo.GetCustomersFromUSA() WHERE CustomerID = 'ZZZZZ' Let’s take a look at the query planquery plan

Updating actual data through UDFs UPDATE dbo.GetCustomersFromUSA() SET CompanyName = 'New Customer' WHERE CustomerID = 'ZZZZZ' Let’s take a look at the query planquery plan

Summary You can bypass some UDFs limitations You can learn to love some undocumented UDFs You know you can create a system UDF And redefine system stored procedures as system UDFs And get extra performance and flexibility by combining UDFs with Indexed views And you can modifying data through UDFs Isn’t this cool?

Ask The Experts Get Your Questions Answered I’ll be at the Ask the Experts area: 2 July 2003: 12:00-14:00 3 July 2003: 11:00-13:00 4 July 2003: 12:00-14:00 You’ll see me around some more times

Community Resources Most Valuable Professional (MVP) Newsgroups Converse online with Microsoft Newsgroups, including Worldwide User Groups Meet and learn with your peers

Thank you! Questions? Download the source code of this session from: You can contact me at:

evaluations evaluations

© 2003 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. MICROSOFT MAKES NO WARRANTIES, EXPRESS OR IMPLIED, IN THIS SUMMARY.