分号连接的sql语句是否具有原子性(具有事务功能)?

浪漫幕末 2008-12-16 09:08:42
用分号连接多条sql语句可以一次执行,但它运行的时候是不是一个事物呢?具不具有事务的原子性特点?请知道的说一下,给个例子我测试一下,谢谢~
...全文
229 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
浪漫幕末 2008-12-17
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 kelph 的回复:]
这个本来就是事务,之所以在中间不会回滚不是因为事务的问题,而是@error返回的是最后一条语句的值。

这个例子是用来说明事务,而非@@ERROR的使用方法。
[/Quote]
我明白你的意思了,你是说即使是事务没有处理好,也是不会回滚的。其实那不能说是事物,只能说它是一个错误的事物,事物应该具有原子性。大概明白你要表达的意思了:简单的“;”号连接sql语句,就算它是一个事务,但缺少必要的处理,所以它不会具有原子性。谢谢你的指点,我明白了。事务必须要自己写,而且需要写正确才有原子性等特点。
kelph 2008-12-16
  • 打赏
  • 举报
回复
这个本来就是事务,之所以在中间不会回滚不是因为事务的问题,而是@error返回的是最后一条语句的值。

这个例子是用来说明事务,而非@@ERROR的使用方法。

浪漫幕末 2008-12-16
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 kelph 的回复:]
SQL codecreate table testtranc(id int primary key)

begin transaction---加与不加的区别
insert testtranc values ('1')
insert testtranc values ('2')
insert testtranc values ('3')
insert testtranc values ('2')

if @@error <> 0
begin
rollback transaction;
return
end
else
begin
commit transaction
return
end

select * from testtranc
[/Quote]

你这个看起来能起到事物作用,其实是由于恰好是最后一个语句错误
如果换成:效果就不一样了。

begin transaction---加与不加的区别
insert testtranc values ('1')
insert testtranc values ('2')

insert testtranc values ('2')
insert testtranc values ('3')
if @@error <> 0
begin
rollback transaction;
return
end
else
begin
commit transaction
return
end

以下是sql中正确的使用@@error方法:(嘿嘿)
用 @@ERROR 检测几条语句的成功
下面的示例取决于 INSERT 和 DELETE 语句的成功操作。局部变量在两条语句后均被设置为 @@ERROR 的值,并且用于此操作的共享错误处理例程中。

USE pubs
GO
DECLARE @del_error int, @ins_error int
-- Start a transaction.
BEGIN TRAN

-- Execute the DELETE statement.
DELETE authors
WHERE au_id = '409-56-7088'

-- Set a variable to the error value for
-- the DELETE statement.
SELECT @del_error = @@ERROR

-- Execute the INSERT statement.
INSERT authors
VALUES('409-56-7008', 'Bennet', 'Abraham', '415 658-9932',
'6223 Bateman St.', 'Berkeley', 'CA', '94705', 1)
-- Set a variable to the error value for
-- the INSERT statement.
SELECT @ins_error = @@ERROR

-- Test the error values.
IF @del_error = 0 AND @ins_error = 0
BEGIN
-- Success. Commit the transaction.
PRINT "The author information has been replaced"
COMMIT TRAN
END
ELSE
BEGIN
-- An error occurred. Indicate which operation(s) failed
-- and roll back the transaction.
IF @del_error <> 0
PRINT "An error occurred during execution of the DELETE
statement."

IF @ins_error <> 0
PRINT "An error occurred during execution of the INSERT
statement."

ROLLBACK TRAN
END
GO

浪漫幕末 2008-12-16
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 kelph 的回复:]
SQL codecreate table testtranc(id int primary key)

begin transaction---加与不加的区别
insert testtranc values ('1')
insert testtranc values ('2')
insert testtranc values ('3')
insert testtranc values ('2')

if @@error <> 0
begin
rollback transaction;
return
end
else
begin
commit transaction
return
end

select * from testtranc
[/Quote]

我觉得你这个事物有问题,
sql里对@@error的解释:由于 @@ERROR 在每一条语句执行后被清除并且重置,应在语句验证后立即检查它,或将其保存到一个局部变量中以备事后查看
必须每一条语句完成都立刻检测@@error值,我认为要定义四个变量来保存4个语句运行的@@error值,只有当他们都为0才提交事物。
浪漫幕末 2008-12-16
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 dawugui 的回复:]
update tb set col1 = 1 ; update tb set col2 = 1

如果第一个update语句出错,第二个update照样执行.

加;号只是表示这句话结束.
[/Quote]

我明白了,也就是sql服务器出现意外导致其中1条没有执行,但后面的仍然会执行。前提是这条接起来的sql本身没有语法错误。我以前是故意放1个语法错误在其中1条里,结果都没执行,还认为那是事物呢。谢谢指点~
kelph 2008-12-16
  • 打赏
  • 举报
回复
create table testtranc(id int primary key)

begin transaction---加与不加的区别
insert testtranc values ('1')
insert testtranc values ('2')
insert testtranc values ('3')
insert testtranc values ('2')

if @@error <> 0
begin
rollback transaction;
return
end
else
begin
commit transaction
return
end

select * from testtranc
dawugui 2008-12-16
  • 打赏
  • 举报
回复
update tb set col1 = 1 ; update tb set col2 = 1

如果第一个update语句出错,第二个update照样执行.

加;号只是表示这句话结束.
浪漫幕末 2008-12-16
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 dawugui 的回复:]
引用楼主 daijun17 的帖子:
用分号连接多条sql语句可以一次执行,但它运行的时候是不是一个事物呢?具不具有事务的原子性特点?请知道的说一下,给个例子我测试一下,谢谢~

不是一个事务.
[/Quote]
谢谢你,效率真是高。能给个例子说明一下吗?
kelph 2008-12-16
  • 打赏
  • 举报
回复
不是一个事务
begin tran 事务开始
therobinliu 2008-12-16
  • 打赏
  • 举报
回复
学习中~~~~~~~~
浪漫幕末 2008-12-16
  • 打赏
  • 举报
回复
补充一下:我觉得那不是一个事物,但有人说是。系统里很多地方都是用这种办法执行多条sql的,现在系统数据有点不一致了,在分析原因。
dawugui 2008-12-16
  • 打赏
  • 举报
回复
[Quote=引用楼主 daijun17 的帖子:]
用分号连接多条sql语句可以一次执行,但它运行的时候是不是一个事物呢?具不具有事务的原子性特点?请知道的说一下,给个例子我测试一下,谢谢~
[/Quote]
不是一个事务.

34,575

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧