一个简单的存储过程:
=====================================================
ALTER PROCEDURE [dbo].[InsertTestAndTestChar]
@Char CHAR(1)
AS
BEGIN BEGIN TRANSACTION BEGIN TRY INSERT INTO [Test]
(
[Code]
)
VALUES
(
@Char
) INSERT INTO [_TestChar]
(
[Code]
)
VALUES
(
@Char
) COMMIT END TRY BEGIN CATCH ROLLBACK END CATCHEND我帮意将表名 TestChar 改为 _TestChar 引起存储过程执行出错。报错如下:(1 row(s) affected)
Msg 208, Level 16, State 1, Procedure InsertTestAndTestChar, Line 24
Invalid object name '_TestChar'.
Msg 266, Level 16, State 2, Procedure InsertTestAndTestChar, Line 24
Transaction count after EXECUTE indicates that a COMMIT or ROLLBACK TRANSACTION statement is missing. Previous count = 1, current count = 2.(1 row(s) affected)最后一个错是什么意思?为什么会报那个错?我已经 try catch 了,为什么还是会报出岂常?我并没有在 try catch 模块里面抛出异常啊。
=====================================================
ALTER PROCEDURE [dbo].[InsertTestAndTestChar]
@Char CHAR(1)
AS
BEGIN BEGIN TRANSACTION BEGIN TRY INSERT INTO [Test]
(
[Code]
)
VALUES
(
@Char
) INSERT INTO [_TestChar]
(
[Code]
)
VALUES
(
@Char
) COMMIT END TRY BEGIN CATCH ROLLBACK END CATCHEND我帮意将表名 TestChar 改为 _TestChar 引起存储过程执行出错。报错如下:(1 row(s) affected)
Msg 208, Level 16, State 1, Procedure InsertTestAndTestChar, Line 24
Invalid object name '_TestChar'.
Msg 266, Level 16, State 2, Procedure InsertTestAndTestChar, Line 24
Transaction count after EXECUTE indicates that a COMMIT or ROLLBACK TRANSACTION statement is missing. Previous count = 1, current count = 2.(1 row(s) affected)最后一个错是什么意思?为什么会报那个错?我已经 try catch 了,为什么还是会报出岂常?我并没有在 try catch 模块里面抛出异常啊。
IF XACT_STATE()<>0 -- 回滚之前要判断事务是否确实启动.
ROLLBACK END CATCHEND
Transaction count after EXECUTE indicates that a COMMIT or ROLLBACK TRANSACTION statement is missing. Previous count = 9, current count = 10.
代码如下:
ALTER PROCEDURE [dbo].[InsertTestAndTestChar]
@Char CHAR(1)
AS
BEGIN BEGIN TRANSACTION BEGIN TRY INSERT INTO [Test]
(
[Code]
)
VALUES
(
@Char
) INSERT INTO [_TestChar]
(
[Code]
)
VALUES
(
@Char
) COMMIT END TRY BEGIN CATCH IF XACT_STATE() <> 0
ROLLBACK END CATCHEND
在后台这个基本是垃圾,不要期待它象前台那样