| 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 WeekDebugging UDFsVolume 2 Number 8 February 10, 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 right now!
Testing and debugging procedural code is made vastly easier when you have a good debugger built into your development tool. SQL Server 2000's Query Analyzer has a stored procedure debugger that makes debugging T-SQL stored procedures pretty easy. Unfortunately, if you pull down the context menu of a UDF, you'll see that the DEBUG menu entry down at the bottom is grayed out. Debugging UDFs has been left out of SQL Server 2000. Or has it? It seems that although the ability to debug a UDF was left out of Query Analyzer, UDF debugging capability exists in the database engine. The question remains, "How to get to this capability and debug that pesky UDF." The answer turns out to be simple: Create a stored procedure that invokes the UDF and debug that. When the time comes to call the UDF, use the Step Into (usually F11) to enter the UDF. To demonstrate the debugging procedure, let's start with the UDF that we want to debug. We'll use udf_TxtNSingleSpace which was published in Velum 1 #52. I include it here for your convenience. Here's the CREATE FUNCTION script:
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE PROC dbo.DEBUG_udf_TxtN_SingleSpace AS
PRINT 'Test 1 ' + CASE WHEN 'A B C D EFGHI J KlN OP ' =
dbo.udf_TxtN_SingleSpace (' A B C D EFGHI J KlN OP ')
THEN 'Worked' ELSE 'ERROR' END
GO
Once DEBUG_udf_TxtN_SingleSpace has been created, go to the Stored Procedures node of the Object Browser and refresh it. DEBUG_udf_TxtN_SingleSpace should now show up. Right click on the name to use the context menu for DEBUG_udf_TxtN_SingleSpace and select the last entry: DEBUG..., which will start the T-SQL debugger on the stored procedure. Since the debugger is starting on the stored procedure, which doesn't have any parameters, there are no start-up parameters to supply, so you can press the execute key and begin the debugging session. Next use the Step Into tool (F11 on the keyboard) and step into the UDF. Now you can debug to figure out what's going wrong with your UDF. Once you identify a change, you'll have to stop and close the debugger, ALTER your UDF and then restart the debugger. It isn't necessary to change DEBUG_udf_TxtN_SingleSpace unless you want to change the test that you're running. If you're having trouble debugging T-SQL, it may have to do with the configuration of your system. There are a variety of prerequisites that must be met before T-SQL debugging works. You might want to take a look at the Microsoft knowledge base article, "INF: Transact-SQL Debugger Limitations and Troubleshooting Tips for SQL Server 2000", 280101 located at this URL:
http://support.microsoft.com/default.aspx?scid=kb;en-us;280101
All-in-all, T-SQL debugging is adequate for the job and miles ahead of debugging with PRINT statements, which you can't have in a UDF anyway. There is one more trick to UDF debugging. If you have Visual Studio.Net, it allows you to debug UDFs directly without creating a DEBUG stored procedure. Chapter 3 of Transact-SQL User-Defined Functions includes a section about debugging UDFs and goes into a bit more detail. It's available on my web site and on Amazon and in many stores.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| |