2016-10-07 14 views
0

これは私のコードです。どのようにエラーを避けるために、自動的に既に格納されているすべてのクエリが発生しました。複数の挿入エラーを実行する方法SQL Serverストアドプロシージャのロールバック挿入テーブル

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, 
+0

あなたが何を求めているのか分かりません。あなたのコードをトランザクションに入れてロールバックすることを求めているだけですか?チェック:https://msdn.microsoft.com/en-gb/library/ms188929.aspx – Tanner

+0

サイドノート:ストアドプロシージャのプレフィックス 'sp_'を使用しないでください**。マイクロソフトは、[*ストアドプロシージャの名前付け*を参照してください](http://msdn.microsoft.com/en-us/library/ms190669%28v=sql.105%29.aspx)、およびあなたはいつか名前衝突のリスクを将来実行します。 [ストアドプロシージャのパフォーマンスにも悪い](http://www.sqlperformance.com/2012/10/t-sql-queries/sp_prefix)単に 'sp_'を避け、他の何かを接頭辞として使うのが最善です。 –

答えて

0

あなたの質問はあまり明確ではありませんが、あなたはこれらの行に沿って何かをしたいと思います。

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 

これは1つの警告で動作します。トランザクションがSP_Storeの場合、SQL Serverのトランザクションをネストできないため、これは正しく動作しません。

また、実際にはSP_接頭辞を避けるか、接頭辞を完全に避けるほうがよいでしょう。 http://sqlperformance.com/2012/10/t-sql-queries/sp_prefix

関連する問題