在查询分析器中执行SQL如何实现回滚?

superren 2007-06-14 04:44:14
有一些修改表字段的语句,想实现在任一条修改出错时回滚的效果

--1、增加问题表dbo.ISS_question字段qNumsOfRow:纵向0 横向>0
alter table dbo.ISS_question add qNumsOfRow int null
--初始化该字段
update dbo.ISS_question set qNumsOfRow = 0 where qNumsOfRow is null

--2、增加问题表dbo.ISS_question分页描述字段nextpageinfo
alter table dbo.ISS_question add nextpageinfo varchar(200) null

--3、增加问卷表dbo.ISS_app是否针对被评对象字段isLimitObj:0否1是
alter table dbo.ISS_app add isLimitObj smallint null
--初始化该字段
update dbo.ISS_app set isLimitObj = 0 where isLimitObj is null

请高手们写出具体代码哦~~~
...全文
363 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
hui_hui_2007 2007-06-15
  • 打赏
  • 举报
回复
create,alter,drop无法实现回滚,立即生效的.
lang8134 2007-06-15
  • 打赏
  • 举报
回复
create,alter,drop,这就是ddl.
不能回滚.
hui_hui_2007 2007-06-15
  • 打赏
  • 举报
回复
SET XACT_ABORT
指定当 Transact-SQL 语句产生运行时错误时,Microsoft® SQL Server™ 是否自动回滚当前事务。

语法
SET XACT_ABORT { ON | OFF }

注释
当 SET XACT_ABORT 为 ON 时,如果 Transact-SQL 语句产生运行时错误,整个事务将终止并回滚。为 OFF 时,只回滚产生错误的 Transact-SQL 语句,而事务将继续进行处理。编译错误(如语法错误)不受 SET XACT_ABORT 的影响。

对于大多数 OLE DB 提供程序(包括 SQL Server),隐性或显式事务中的数据修改语句必须将 XACT_ABORT 设置为 ON。唯一不需要该选项的情况是提供程序支持嵌套事务时。有关更多信息,请参见分布式查询和分布式事务。

SET XACT_ABORT 的设置是在执行或运行时设置,而不是在分析时设置。
superren 2007-06-15
  • 打赏
  • 举报
回复
我当然知道rollback了,可是写上了没效果,出错时不会取消上面的更新
begin tran mytran
alter table dbo.ISS_question add qNumsOfRow int null
alter table dbo.ISS_question add nextpageinfo varchar(200) null
alter table dbo.ISS_app add isLimitObj smallint null
go
if @@error<>0 begin
rollback tran mytran
end
else begin
commit tran mytran
end

没效果啊
superren 2007-06-15
  • 打赏
  • 举报
回复
ddl啥?
lang8134 2007-06-14
  • 打赏
  • 举报
回复
rollback
ddl 不能回滚

34,591

社区成员

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

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