First Post in this series: SQL Advent Calendar 2013-Day 1- Placeholders in SQL Prompt Snippets
As I was hanging our family Advent Calendar up, I thought I would make one for you, my readers. I’ll be sharing my scripts from two of my favorite products as well as tips on how to create your own.
Three French Hens, I mean User Defined Functions
There are three kinds of User Defined Functions that you can create in SQL Server.
- Scalar Functions
- Multi-line Table Functions
- In-Line Table Functions
They all have a purpose but unfortunately the first two listed above are misunderstood and misused the most. Below are a couple of articles on the differences and the performance problems that can be caused by Scalar and Multi-line Table Functions.
- Execution Plan for a User Defined Function by Grant Fritchey
- Comparing Inline and Multi-Statement Table-Valued Functions by Wayne Sheffield
I created this snippet called NewFun for SQL Prompt when I was first learning the differences between these function types, and I still use to today. Why? It saves key strokes.
USE <DatabaseName, string,$DBNAME$> GO --Use Shift + Ctrl + M to set Template Parameters IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'<schemaowner,string,dbo>.<sprocname,string,>') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT')) DROP FUNCTION <schemaowner,string,dbo>.<sprocname,string,> GO /* *********************************************************************************** Purpose: [Brief Description] Notes: Sample: EXEC <schemaowner,string,dbo>.<sprocname,string,> [Parameters] Author: $USER$ Date: $DATE$ Revision History (Change Date) (Author) (Description of Change) ----------------------------------------------------------------- ************************************************************************************ */ CREATE FUNCTION <schemaowner,string,dbo>.<sprocname,string,> ( --Insert Parameters ) RETURNS Table AS RETURN ( $CURSOR$ ) GO
Note: You can download this snippet from my Script Library under Resources. It’s called NewFun. |
Previous Post In Series: SQL Advent Calendar Day -15 – Naming Default Constraints
Next Post In Series: SQL Advent Calendar – Day 17 – Now, Which Tables Still Need a Trigger?
Hi Mickey,
Thanks for the link back to my article.
BTW, I love how your script includes that RETURN $CURSOR$. Sometimes, I think that is exactly what they do! 🙂
(Seriously, I do know what it’s used for.)
That is pretty funny, especially since I shutter at the mention of using cursors. 🙂
Mickey