One generous Professional Underground for SQL Server 2011 presenter was kind enough to allow publication of their very interesting and useful script, so here it is. Thanks, Mister Anonymous!!
Warning: Don’t be an idiot; this CLEARLY isn’t meant for production use.
Warning 2: Remember your NDA, PUSSies…we don’t discuss what was done, or who did what, OR ELSE YOU WILL SUFFER SERIOUS CONSEQUENCES. And then we can’t do it again.
-- Code sample and test cases: USE OhNoYouDidnt GO CREATE TRIGGER talkToTheHand ON DATABASE FOR CREATE_FUNCTION AS /* No-arguments, problem solving trigger for scalar UDF's Don't try this at home, we're what you call "Experts" PUSS 2011 Mister Anonymous */ DECLARE @DidICatchYouFucker BIT ; SET @DidICatchYouFucker = 0 ; DECLARE @TextOfYourEffinFunction NVARCHAR(MAX) ; SELECT @TextOfYourEffinFunction = EVENTDATA().value('(/EVENT_INSTANCE/TSQLCommand/CommandText)[1]', 'nvarchar(max)') ; IF @TextOfYourEffinFunction NOT LIKE '%RETURNS TABLE%' SET @DidICatchYouFucker = 1 ; IF @DidICatchYouFucker = 1 BEGIN DECLARE @theFixForThis NVARCHAR(MAX) ; SET @theFixForThis = 'DROP FUNCTION /* take this, dickhead */ ' + QUOTENAME(EVENTDATA().value('(/EVENT_INSTANCE/SchemaName)[1]', 'varchar(25)')) + '.' + QUOTENAME(EVENTDATA().value('(/EVENT_INSTANCE/ObjectName)[1]', 'varchar(256)')) ; EXEC ( @theFixForThis ) ; END ; GO CREATE FUNCTION dbo.myFunction ( @someParameter INT ) RETURNS INT AS BEGIN /* Hurray For Reusable Code! I'm so Smart I can't Stand Myself */ RETURN 3 ; END ; GO CREATE FUNCTION dbo.myFunction2 ( @someParameter INT ) RETURNS TABLE AS /* Inline TVF FTW! */ RETURN ( SELECT 3 AS value ) ; GO /* DROP FUNCTION dbo.myFunction DROP FUNCTION dbo.myFunction2 GO DISABLE TRIGGER [talkToTheHand] ON DATABASE ; DROP TRIGGER [talkToTheHand] ON DATABASE ; GO */
P.S. The really great thing? Red-gate’s SQL Prompt formatted this without complaint. It’s solid SQL code.