sqlCmd下的备份还原执行sql脚本和事务等处理

SqlCmd -S ./SqlExpress

//备份等命令

 

      //控制单用户访问和多用户访问

      //USE [master]

      //GO

      //ALTER DATABASE [BidFileSell] SET  SINGLE_USER WITH ROLLBACK IMMEDIATE

      //ALTER DATABASE [BidFileSell] SET  MULTI_USER WITH NO_WAIT

      //GO

 

//备份恢复数据库

 

'说明,备份恢复数据库

SQLCMD -S ./sqlexpress -U sa -P 000000 -d master -Q"BACKUP DATABASE test to disk='c:

 

/aa/aaa.bak'"

SQLCMD -S ./sqlexpress -U sa -P 000000 -d master -Q"RESTORE DATABASE test from 

 

disk='c:/aa/aaa.bak'"

'说明 ,在D:下database文件夹,在database下建立data

sqlcmd -S ./sqlexpress -i c:/createtable.sql

'重复几次,5次

sqlcmd -S ./sqlexpress -i c:/createdata.sql

 

//运行sql

//存储过程

sp_help

sp_helptext

 

//列出字段

 SELECT Name FROM SysColumns WHERE id=Object_Id('TableName')

 

//切换数据库

  use databasename;

  go

//查看表

  select name from sys.tables;

 

 

create table bbb(id integer);

//测试事务

declare @v_c as integer;

set @v_c=1;

begin tran

while @v_c<50000

begin

insert into bbb(id) values(@v_c);

set @v_c=@v_c+1;

end

if @@error>0 

rollback tran

else

commit tran

go

 

 

 

//测试事务2

 

create table fff(id integer primary key);

 

declare @v_c as integer;

declare @e_c as integer;

set @e_c=0;

set @v_c=1;

begin tran

while @v_c<8

begin

insert into fff(id) values(1);

if @@error<>0 

 set @e_c=@e_c+1

set @v_c=@v_c+1;

end

if @e_c>0 

rollback tran

else

commit tran

go

 

//或者加return返回,这样写

 

declare @v_c as integer;

set @v_c=1;

begin tran

while @v_c<8

begin

insert into fff(id) values(1);

if @@error<>0 

begin

 rollback tran

 return 

end

set @v_c=@v_c+1;

end

commit tran

go

你可能感兴趣的:(sqlCmd下的备份还原执行sql脚本和事务等处理)