是否可以使用事务创建SQL SERVER函数

时间:2011-03-31 13:27:57

标签: sql-server tsql function transactions try-catch

我有一个简单的脚本,基本上做了很多db操作。现在我需要升级数据库,我需要在同一个事务中包含一个函数创建。

BEGIN TRY --Start the Try Block..

    BEGIN TRANSACTION -- Start the transaction..
        -- HERE I need to add one function like
        IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[FuctionTest]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[FuctionTest]
GO
CREATE FUNCTION FuctionTest 
(   
    @inputVal int
)
RETURNS int
AS
BEGIN
    RETURN (Select Id from [dbo].[test] where Id=@inputVal)
END

        UPDATE MyChecking SET Amount = Amount - @Amount

            WHERE AccountNum = @AccountNum

        UPDATE MySavings SET Amount = Amount + @Amount

            WHERE AccountNum = @AccountNum

    COMMIT TRAN -- Transaction Success!

END TRY

BEGIN CATCH

    IF @@TRANCOUNT > 0

        ROLLBACK TRAN --RollBack in case of Error

我将如何完成这项工作?我不想使用单独的脚本来创建函数。

1 个答案:

答案 0 :(得分:5)

是的,请使用EXEC('...'),如:

--list all functions named like '%test_function%', which will not find any
SELECT [name] FROM sys.objects WHERE [name] like '%test_function%'

--create a function named 'test_function1'
exec ('create function [dbo].[test_function1] (@x int) returns varchar(50) as BEGIN return convert(varchar(50),@x)+''!'' END')

begin transaction

--create a function 'test_function2', which will be rolled back
exec ('create function [dbo].[test_function2] (@x int) returns varchar(50) as BEGIN return convert(varchar(50),@x)+''!!!!'' END')

rollback

--show the functions named like '%test_function%'
--which will only find 'test_function1', since 'test_function2'
--was rolled back
SELECT [name] FROM sys.objects WHERE [name] like '%test_function%'

输出:

name
-----------------------

(0 row(s) affected)

name
-------------------------
test_function1

(1 row(s) affected)

编辑以显示开始尝试阻止....

BEGIN TRY

    --list all functions named like '%test_function%', which will not find any
    SELECT [name] FROM sys.objects WHERE [name] like '%test_function%'

    --create a function named 'test_function1'
    exec ('create function [dbo].[test_function1] (@x int) returns varchar(50) as BEGIN return convert(varchar(50),@x)+''!'' END')

    begin transaction

    --create a function 'test_function2', which will be rolled back
    exec ('create function [dbo].[test_function2] (@x int) returns varchar(50) as BEGIN return convert(varchar(50),@x)+''!!!!'' END')

    RAISERROR('force catch!',16,1) --send control to the BEGIN CATCH block

    --should never get here
    COMMIT 

END TRY
BEGIN CATCH

    IF XACT_STATE()!=0
    BEGIN
        ROLLBACK TRANSACTION
    END
    PRINT CASE WHEN ERROR_NUMBER() IS NOT NULL THEN 'Msg '+CONVERT(varchar(30),ERROR_NUMBER()) ELSE '' END+CASE WHEN ERROR_SEVERITY() IS NOT NULL THEN ', Level '+CONVERT(varchar(30),ERROR_SEVERITY()) ELSE '' END+CASE WHEN ERROR_STATE() IS NOT NULL THEN ', State '+CONVERT(varchar(30),ERROR_STATE()) ELSE '' END+CASE WHEN ERROR_PROCEDURE() IS NOT NULL THEN ', Procedure '+ERROR_PROCEDURE() ELSE '' END+CASE WHEN ERROR_LINE() IS NOT NULL THEN ', Line '+CONVERT(varchar(30),ERROR_LINE()) ELSE '' END+CASE WHEN ERROR_MESSAGE() IS NOT NULL THEN ', '+ERROR_MESSAGE() ELSE '' END
END CATCH


--show the functions named like '%test_function%'
--which will only find 'test_function1', since 'test_function2'
--was rolled back
SELECT [name] FROM sys.objects WHERE [name] like '%test_function%'

输出:

name
---------------------------------------------------

(0 row(s) affected)

Msg 50000, Level 16, State 1, Line 14, force catch!
name
---------------------------------------------------
test_function1

(1 row(s) affected)