说明:灰色
以下介绍的是(显式事务) /*建库*/ USE master GO IF EXISTS(SELECT * FROM SYSDATABASES WHERE NAME='bankDB') DROP DATABASE bankDB EXEC XP_CMDSHELL 'MD D:\BANK' --创建存放数据库文件夹 CREATE DATABASE bankDB ON ( NAME = 'bankDB_data', FILENAME = 'D:\BANK\bankDB_data.MDF', SIZE = 10MB, FILEGROWTH = 15% ) LOG ON ( NAME = 'bankDB_LOG', FILENAME = 'D:\BANK\bankDB_LOG.LDF', SIZE = 10MB, FILEGROWTH = 15% ) GO /*建表*/ USE bankDB GO IF EXISTS (SELECT * FROM SYSOBJECTS WHERE NAME = 'TEST') DROP TABLE TEST CREATE TABLE TEST ( TNAME VARCHAR(8) NOT NULL PRIMARY KEY, --主键 TYUER MONEY CHECK (TYUER>=1) --存款余额不能小于1 ) GO --为表插入数据 INSERT TEST VALUES ('A',1000) INSERT TEST VALUES ('B',1) GO DECLARE @errorSun INT --定义错误计数器 UPDATE TEST SET TYUER=TYUER-10 WHERE TNAME='A' --事务操作SQL语句 UPDATE TEST SET TYUER=TYUER+10 WHERE TNAME='B' -- IF @errorSun<>0 BEGIN PRINT '有错误,回滚' ROLLBACK TRANSACTION--事务回滚语句 ELSE BEGIN PRINT '成功,提交' COMMIT TRANSACTION--事务提交语句 GO 提交事物:COMMIT TRANSACTION 回滚事务:ROLLBACK TRANSACTION ②一致性(Consistemcy): ③隔离性(Isolation): ④持久性(Durability): ROLLBACK TRANSACTION 保存点名称 |