mysql 存储过程中update影响行数为0,回滚
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql 存储过程中update影响行数为0,回滚相关的知识,希望对你有一定的参考价值。
请高手指教一个问题,在mysql的存储过程中update更新影响行数为0,怎么获取到这个0,并回滚!
谁帮忙指教一下,急求
-- 开始事务
start transaction;
call setCoin(zjAmount, 0, `uid`, liqType, `type`, info, _betId, serializeId, '');
update blast_bets set lotteryNo=_kjData, zjCount=_zjCount, bonus=zjAmount, fanDianAmount=_fanDianAmountSelf where id=_betId;
-- 提交事务
commit;
使用FOUND_ROWS() 获得影响的行数,再用IF判断是否等于0就行了。
start transaction;
call setCoin(zjAmount, 0, `uid`, liqType, `type`, info, _betId, serializeId, '');
update blast_bets set lotteryNo=_kjData, zjCount=_zjCount, bonus=zjAmount, fanDianAmount=_fanDianAmountSelf where id=_betId;
IF FOUND_ROWS() = 0 THEN
ROLLBACK;
ELSE
COMMIT;
END IF;
commit; 参考技术A 在存储过程中使用ROW_COUNT()
mysql> select * from a1;
-> //
+------+-------+
| id | name |
+------+-------+
| 1 | timac |
+------+-------+
1 row in set (0.00 sec)
mysql> \d ;
mysql> update a1 set name='king' where id=1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select row_count();
+-------------+
| row_count() |
+-------------+
| 1 |
+-------------+
1 row in set (0.00 sec)
mysql> insert into a1 values(2,'K1'),(3,'K2');
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select row_count();
+-------------+
| row_count() |
+-------------+
| 2 |
+-------------+
1 row in set (0.00 sec)
mysql> update a1 set name='kings' where id=4;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0 Changed: 0 Warnings: 0
mysql> select row_count();
+-------------+
| row_count() |
+-------------+
| 0 |
+-------------+
1 row in set (0.00 sec)
只有返回的row_count()值大小还判断是否成功
返回受 UPDATE 语句影响的行数
【中文标题】返回受 UPDATE 语句影响的行数【英文标题】:Return number of rows affected by UPDATE statements 【发布时间】:2009-07-09 11:18:38 【问题描述】:如何在存储过程 (SQL Server 2005) 中获取受 UPDATE 查询影响的行数作为结果集。例如
CREATE PROCEDURE UpdateTables
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
UPDATE Table1 Set Column = 0 WHERE Column IS NULL
UPDATE Table2 Set Column = 0 WHERE Column IS NULL
UPDATE Table3 Set Column = 0 WHERE Column IS NULL
UPDATE Table4 Set Column = 0 WHERE Column IS NULL
END
然后返回:
Table1 Table2 Table3 Table4
32 45 0 3
【问题讨论】:
【参考方案1】:CREATE PROCEDURE UpdateTables
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
DECLARE @RowCount1 INTEGER
DECLARE @RowCount2 INTEGER
DECLARE @RowCount3 INTEGER
DECLARE @RowCount4 INTEGER
UPDATE Table1 Set Column = 0 WHERE Column IS NULL
SELECT @RowCount1 = @@ROWCOUNT
UPDATE Table2 Set Column = 0 WHERE Column IS NULL
SELECT @RowCount2 = @@ROWCOUNT
UPDATE Table3 Set Column = 0 WHERE Column IS NULL
SELECT @RowCount3 = @@ROWCOUNT
UPDATE Table4 Set Column = 0 WHERE Column IS NULL
SELECT @RowCount4 = @@ROWCOUNT
SELECT @RowCount1 AS Table1, @RowCount2 AS Table2, @RowCount3 AS Table3, @RowCount4 AS Table4
END
【讨论】:
【参考方案2】:这正是 SQL Server 2005 及更高版本中的 OUTPUT
子句的出色之处。
示例
CREATE TABLE [dbo].[test_table](
[LockId] [int] IDENTITY(1,1) NOT NULL,
[StartTime] [datetime] NULL,
[EndTime] [datetime] NULL,
PRIMARY KEY CLUSTERED
(
[LockId] ASC
) ON [PRIMARY]
) ON [PRIMARY]
INSERT INTO test_table(StartTime, EndTime)
VALUES('2009 JUL 07','2009 JUL 07')
INSERT INTO test_table(StartTime, EndTime)
VALUES('2009 JUL 08','2009 JUL 08')
INSERT INTO test_table(StartTime, EndTime)
VALUES('2009 JUL 09','2009 JUL 09')
INSERT INTO test_table(StartTime, EndTime)
VALUES('2009 JUL 10','2009 JUL 10')
INSERT INTO test_table(StartTime, EndTime)
VALUES('2009 JUL 11','2009 JUL 11')
INSERT INTO test_table(StartTime, EndTime)
VALUES('2009 JUL 12','2009 JUL 12')
INSERT INTO test_table(StartTime, EndTime)
VALUES('2009 JUL 13','2009 JUL 13')
UPDATE test_table
SET StartTime = '2011 JUL 01'
OUTPUT INSERTED.* -- INSERTED reflect the value after the UPDATE, INSERT, or MERGE statement is completed
WHERE
StartTime > '2009 JUL 09'
返回以下结果
LockId StartTime EndTime
-------------------------------------------------------
4 2011-07-01 00:00:00.000 2009-07-10 00:00:00.000
5 2011-07-01 00:00:00.000 2009-07-11 00:00:00.000
6 2011-07-01 00:00:00.000 2009-07-12 00:00:00.000
7 2011-07-01 00:00:00.000 2009-07-13 00:00:00.000
在您的特定情况下,由于您不能对 OUTPUT
使用聚合函数,因此您需要在表变量或临时表中捕获 INSERTED.*
的输出并计算记录。例如,
DECLARE @temp TABLE (
[LockId] [int],
[StartTime] [datetime] NULL,
[EndTime] [datetime] NULL
)
UPDATE test_table
SET StartTime = '2011 JUL 01'
OUTPUT INSERTED.* INTO @temp
WHERE
StartTime > '2009 JUL 09'
-- now get the count of affected records
SELECT COUNT(*) FROM @temp
【讨论】:
OUTPUT INSERTED.* INTO @temp 正是我想要的【参考方案3】:您可能需要随时收集统计数据,但@@ROWCOUNT
捕捉到了这一点:
declare @Fish table (
Name varchar(32)
)
insert into @Fish values ('Cod')
insert into @Fish values ('Salmon')
insert into @Fish values ('Butterfish')
update @Fish set Name = 'LurpackFish' where Name = 'Butterfish'
select @@ROWCOUNT --gives 1
update @Fish set Name = 'Dinner'
select @@ROWCOUNT -- gives 3
【讨论】:
以上是关于mysql 存储过程中update影响行数为0,回滚的主要内容,如果未能解决你的问题,请参考以下文章