Sqlserver事务备份和还原的实例代码(必看)
废话不多说,直接上代码
createdatabasemydb
usemydb
go
createtableaccount(
idvarchar(16),
namevarchar(16),
balancefloat
)
go
select*fromaccount
insertintoaccount(id,name,balance)values('620101','liyong',300)
insertintoaccount(id,name,balance)values('620106','mali',400)
--insertintoaccount(id,name,balance)values('620009','chenying',800)
insertintoaccount(id,name,balance)values('646009','chenying',800)
--deletefromaccountwhereid='620009'
go
updateaccountsetbalance=balance-1000whereid='620101'
updateaccountsetbalance=balance+1000whereid='620106'
--消息547,级别16,状态0,第1行
--UPDATE语句与CHECK约束"CK_Blance"冲突。该冲突发生于数据库"mydb",表"dbo.account",column'balance'。
--语句已终止。
go
--altertableaccount
--alterCOlumnbalanceint
go
altertableaccount
addconstraintCK_Blancecheck(balance>=0)
go
altertableaccount
dropconstraintCK_Blance
--定一个事务
--从liyong扣钱往mali加钱
begintransaction
updateaccountsetbalance=balance-1000whereid='620101'
if((selectbalanceoutputfromaccountwhereid='620101')<0)
begin
PRINT('余额不足!');
ROLLBACK;
end
else
begin
updateaccountsetbalance=balance+1000whereid='620106'
commit;
PRINT('转账成功!');
end
go
sp_help
--备份设备
sp_addumpdevice'disk','xk_bak','d:\xk_bak'
--备份数据库
backupdatabasemydb
toxk_bak
--还原数据库
restoredatabasemydbfromdisk='d:\xk_bak'
withreplace;--覆盖
以上这篇Sqlserver事务备份和还原的实例代码(必看)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。
