简体   繁体   English

无法在SQL Server 2008中将事务与Go语句一起使用

[英]Unable to Use Transactions with Go statement in SQL Server 2008

BEGIN TRY
    BEGIN TRANSACTION
SET ANSI_NULLS ON
Go

SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION dbo.RerurnStaticValue
(
@value nvarchar(10)
)
RETURNS varchar(max)
AS 
   BEGIN

      DECLARE
         @ReturnValue nvarchar(10)
         SET @ReturnValue = @value
         RETURN @ReturnValue
    END  
        COMMIT TRAN -- Transaction Success!
END TRY
BEGIN CATCH
    IF @@TRANCOUNT > 0
        ROLLBACK TRAN --RollBack in case of Error
    select ERROR_MESSAGE()
END CATCH

I am preparing a very long script and try to implement the transaction in the script so in case if there will be any error in my script it will not effect my database. 我正在准备一个很长的脚本,并尝试在脚本中实现事务,因此,如果脚本中有任何错误,则不会影响数据库。 But I am getting the error Create function must be the only statement in the batch when implementing transactions. 但是我收到错误消息在执行事务时, Create函数必须是批处理中的唯一语句

Please help. 请帮忙。

I think the documentation on functions is pretty clear: 我认为有关功能的文档非常清楚:

User-defined functions cannot be used to perform actions that modify the database state. 用户定义的函数不能用于执行修改数据库状态的操作。

COMMIT / ROLLBACK definitely fall into this category. COMMIT / ROLLBACK绝对属于这一类。

Use a stored procedure instead. 请改用存储过程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM