| Business Application Development with: SQL Server, C#, VB, VB.Net, ASP, ASP.Net, and XML |
| N | S |
Novick Software Management • Design • Programming • Training • Consulting |
|
|
News Links Schedule Site Map Contact |
|
SQL Server T-SQL User-Defined Function of the WeekUse Query Analyzer Templates to Speed Code CreationVolume 2 Number 50 December 21, 2004Check out the UDF Frequently Asked Questions at:
http://www.novicksoftware.com/UdfOfWeek/UDF_FAQ.htm
Transact-SQL User-Defined Functions has been published! Take a look at it now! This article is a little different from most others in the series in that instead of offering a UDF you're going to see an easier way to create UDFs when using SQL Query Analyzer, or Query Analyzer for short. Query Analyzer offers a template feature that acts as an aid in producing all types of SQL code. There are templates for stored procedures, triggers, and user-defined functions. There are also templates for using some system stored procedures such as the ones to manage extended properties. To use a template do the following:
Here's the standard template for an inline UDF: -- ============================================= -- Create inline function (IF) -- ============================================= IF EXISTS (SELECT * FROM sysobjects WHERE name = N' As you can see it presumes two parameters. Of course, you'll probably have less or more than two but it gets you started. Templates are stored in TQL files. What Query Analyzer is doing when you start (step A above) is to copy the TQL file into the new window.
Here's my template for an Inline UDF: SET QUOTED_IDENTIFIER ON SET ANSI_NULLS ON GO CREATE FUNCTION dbo. I've made it different from the standard template to reflect the way that I think UDFs should be written. For example, most of the template is the header comment. That is I think UDFs should always be documented. I also format UDFs differently than Query Analyzer's template. I use what's called Separator First Formatting (SFF), which places separators at the front of a line instead of at the end. Of course, you already know that because you've been reading this newsletter for a while. Copy the template into a text processor such as Notepad and save it as a TQL file. TQL files are stored in the SQL Server code directory tree in the directory \Microsoft SQL Server\80\Tools\Templates\SQL Query Analyzer. I suggest that you put the template above into the "Create Function" directory under the name "TSQL UDFs Create Inline Function.TQL". The text file is available for download here. This is the last issue of Volume 2 of the T-SQL UDF of the Week Newsletter. I'm going to take the holiday week off and retool for the next volume. The title of the newsletter is going to change but I still don't know the new title. So don't be surprised by the new title. Before that happens please add novicksoftware.com to your safe senders domains in your spam blocker. The new newsletter is going to expand the scope of what it covers. There will still be plenty of UDFs and UDF related topics but it's going to include more topics on stored procedures, triggers, and code written in .Net. The switch is inevitable, the production version of SQL Server 2005 will arrive some time in 2005. Before I go, I'd like to wish you Happy Holidays and a Joyous New Year. Andy Do you have a great UDF that you'd like to share? Or maybe you
have a T-SQL problem that you think could be solved by a UDF
but you don't know how? Send them to:
UDF@NovickSoftware.com
|
|
|
Copyright © 2003-2008 Novick Software, Inc. | Terms of Use | Privacy Policy | Nice Things People Say| |