| 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 WeekSee the status of TriggersVolume 2 Number 7 February 3, 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!
Over the last few weeks, I've had reason to write a few triggers. They're sort of fun to write because they're kind of difficult to test. You have to make changes to the underlying tables to get them to run.
But there are times when you might not want a trigger to run and SQL Server allows them to be disabled an re-enabled with the ALTER TABLE statement. As you turn them on and off, it's possible to loose track of their status and this week's UDF,
udf_Tbl_TriggerStatusTAB helps out by showing the current status of triggers on a group of tables that match a table name pattern. The pattern can be omitted to see the status of all triggers.
Here's the CREATE FUNCTION script:
SET QUOTED_IDENTIFIER ON
GO
SET
ANSI_NULLS ON
GO
ALTER
FUNCTION
dbo.udf_Tbl_TriggerStatusTAB (
@TABLENamePattern as sysname =
NULL --
Tables to show,
--
NULL for all
) RETURNS TABLE
-- No schemabinding due to use of system
tables.
/*
* Shows the enabled/disabled status of triggers that match
* the table name pattern. Use NULL for triggers on all tables
* or a pattern for the LIKE operator to match names.
*
* Example:
select * from udf_Tbl_TriggerStatusTAB(null)
*
* History:
* When Who Description
* ------------- ------- ----------------------------------------
* 2003-11-10 ASN Initial Coding
*
* © Copyright 2003 Andrew Novick http://www.NovickSoftware.com
* You may use this function in any of your SQL Server databases
* including databases that you sell, so long as they contain
* other unrelated database objects. You may not publish this
* UDF either in print or electronically.
* Published in the T-SQL UDF of the Week Vol 2 #7 2/3/04
http://www.NovickSoftware.com/UDFofWeek/UDFofWeek.htm
****************************************************************/
AS RETURN
SELECT TOP 100
PERCENT WITH TIES
T.[name] as TableName
, TR.[Name] as TriggerName
, CASE WHEN 1=OBJECTPROPERTY(TR.[id],
'ExecIsTriggerDisabled')
THEN 'Disabled'
ELSE 'Enabled'
END Status
FROM sysobjects T
INNER JOIN sysobjects
TR
on t.[ID] =
TR.parent_obj
WHERE (T.xtype = 'U'
or T.XType = 'V')
AND (@TableNamePattern
IS NULL OR T.[name]
LIKE @TableNamePattern)
AND (TR.xtype = 'TR')
ORDER BY T.[name]
, TR.[name]
GO
GRANT SELECT on
udf_TBL_TriggerStatusTAB to PUBLIC
GO Start with some cleanup of previous tests: 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| |