【问题标题】:How to Execute Multiple Insertion Error Happend RollBack Inserted Table in SQL Server stored procedure如何在 SQL Server 存储过程中执行多次插入错误发生回滚插入的表
【发布时间】:2016-10-07 13:56:06
【问题描述】:

这是我的代码。如何避免错误发生任何查询自动回滚已存储。

insert into muser(UserKey, Email, UserPassword) 
values(@Key, @Useremail, 'test')

set @UserId = SCOPE_IDENTITY()
set @Key = NEWID()

insert into  mUserProfile(UserProfileKey, UserId, UserEmail)
values(@Key, @UserId, @Useremail)

exec SP_Store @Useremail, @ClientId,

【问题讨论】:

标签: sql-server try-catch


【解决方案1】:

你的问题不是很清楚,但我认为你想要一些类似的东西。

begin transaction

begin try

    insert into muser(UserKey,Email,UserPassword) 
    values(@Key,@Useremail,'test')

    set @UserId= SCOPE_IDENTITY()
    set @Key =NEWID()


    insert into  mUserProfile(UserProfileKey,UserId,UserEmail)
    values(@Key,@UserId,@Useremail)

    exec SP_Store @Useremail,@ClientId --You should avoid the SP_ prefix. 

    commit transaction
end try
begin catch
    --Report the error here, do NOT silently rollback you transaction
    rollback transaction
end catch

这应该有一个警告。如果你有一个事务是 SP_Store 这将无法正常工作,因为你不能在 sql server 中嵌套事务。

此外,您确实避免使用 SP_ 前缀,甚至更好地完全避免使用前缀。 http://sqlperformance.com/2012/10/t-sql-queries/sp_prefix

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-10
    • 1970-01-01
    • 2017-10-03
    相关资源
    最近更新 更多