Sqlserver事务备份和还原的实例代码(必看)
作者:jingxian 时间:2024-01-23 18:44:14
废话不多说,直接上代码
create database mydb
use mydb
go
create table account(
id varchar(16),
name varchar(16),
balance float
)
go
select * from account
insert into account(id, name, balance) values('620101', 'liyong', 300)
insert into account(id, name, balance) values('620106', 'mali', 400)
--insert into account(id, name, balance) values('620009', 'chenying', 800)
insert into account(id, name, balance) values('646009', 'chenying', 800)
--delete from account where id = '620009'
go
update account set balance = balance - 1000 where id = '620101'
update account set balance = balance + 1000 where id = '620106'
--消息 547,级别 16,状态 0,第 1 行
--UPDATE 语句与 CHECK 约束"CK_Blance"冲突。该冲突发生于数据库"mydb",表"dbo.account", column 'balance'。
--语句已终止。
go
--alter table account
--alter COlumn balance int
go
alter table account
add constraint CK_Blance check(balance >= 0)
go
alter table account
drop constraint CK_Blance
--定一个事务
--从liyong扣钱往mali加钱
begin transaction
update account set balance = balance - 1000 where id = '620101'
if((select balance output from account where id = '620101') < 0)
begin
PRINT('余额不足!');
ROLLBACK;
end
else
begin
update account set balance = balance + 1000 where id = '620106'
commit;
PRINT('转账成功!');
end
go
sp_help
--备份设备
sp_addumpdevice 'disk', 'xk_bak' ,'d:\xk_bak'
--备份数据库
backup database mydb
to xk_bak
--还原数据库
restore database mydb from disk = 'd:\xk_bak'
with replace; --覆盖
标签:sqlserver,备份,还原
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python查找算法之分块查找算法的实现
2023-06-26 20:25:34
![](https://img.aspxhome.com/file/2023/2/115222_0s.png)
Python字符串常规操作小结
2023-12-02 08:33:23
![](https://img.aspxhome.com/file/2023/2/66322_0s.png)
Go事务中止时是否真的结束事务解析
2023-07-07 11:35:35
PostgreSQL COALESCE使用方法代码解析
2024-01-28 19:55:01
Python调用两个机器人聊天的实战
2021-09-30 23:10:52
![](https://img.aspxhome.com/file/2023/9/124639_0s.png)
使用python进行波形及频谱绘制的方法
2023-02-07 02:48:58
Python:二维列表下标互换方式(矩阵转置)
2022-07-09 13:29:58
完美解决Python2操作中文名文件乱码的问题
2022-12-12 11:29:46
![](https://img.aspxhome.com/file/2023/2/90832_0s.jpg)
PHP实现无限极分类的两种方式示例【递归和引用方式】
2023-11-15 18:26:33
关于vue中element-ui form或table lable换行的问题
2023-07-02 17:07:31
![](https://img.aspxhome.com/file/2023/7/139937_0s.png)
python实现扫雷游戏
2022-05-19 00:59:01
超常用的PHP正则表达式收集整理
2024-05-03 15:35:57
Javascript的错还是浏览器的问题——2009年为何显示为109年
2009-01-11 18:19:00
JavaScript 数组方法filter与reduce
2024-04-29 13:14:38
golang 自旋锁的实现
2024-05-02 16:24:03
.net 6精简版webapi教程及热重载、代码自动反编译演示
2024-05-05 09:13:12
![](https://img.aspxhome.com/file/2023/0/129800_0s.jpg)
Go语言文件读取的一些总结
2024-04-27 15:30:45
![](https://img.aspxhome.com/file/2023/5/134205_0s.png)
Python实现线程池代码分享
2021-09-24 18:13:43
设计哲学与跨界
2009-08-18 12:25:00
python实现抖音视频批量下载
2023-12-06 10:30:20
![](https://img.aspxhome.com/file/2023/8/97698_0s.gif)