bat 操做數據庫(附加,分離,刪除,還原)

 

BAT代碼:
sql

@echo off 
Title DataBase                                              
Color 0A   
:caozuo  
echo.  
echo ═══════════════════════════════════════
echo 【數據庫操做】:      
echo    1.刪除數據庫  
echo    2.還原數據庫     
echo    3.操做3
echo    4.操做4   
echo    5.操做5 
set /p n=輸入操做號: 
if "%n%"=="" cls&goto :caozuo 
if "%n%"=="1" call :1 
if "%n%"=="2" call :2 
if "%n%"=="3" call :3 
if "%n%"=="4" call :4 
if "%n%"=="5" call :5 
if /i "%n%"=="n" exit 
pause 
goto :eof 

:1 
echo. 刪除數據庫開始…………
@osql.exe /Usa /Psa@123456 /Slocalhost /dmaster /i C:\Users\310171611\Desktop\142\database\DropDB.sql
echo. 刪除數據庫結束…………
goto :caozuo 

:2 
echo. 還原數據庫開始…………
@osql.exe /Usa /Psa@123456 /Slocalhost /dmaster /i C:\Users\310171611\Desktop\142\database\RestoreDB.sql
echo. 還原數據庫結束…………
goto :caozuo  
goto :caozuo 

:3 
echo 操做3 
goto :caozuo 

:4
echo 操做4 
goto :caozuo

:5 
echo 操做5 
goto :caozuo


bat文件中調用的是 dos命令 osql,你們對應的修改相應的連接字符串便可數據庫

RestoreDB.sql,DropDB.sql 爲經常使用的sql文件,能夠換成任意的sqlspa

 DropDB.sql 3d

use master
go
alter database dbEMS set single_user with rollback immediate
--將數據庫回滾到原始配置狀態
go
drop database dbEMS--刪除數據庫
GO
use master
go
alter database dbEMSConfig set single_user with rollback immediate
--將數據庫回滾到原始配置狀態
go
drop database dbEMSConfig--刪除數據庫
GO

 

RestoreDB.sqlcode

 

 

USE MASTER 
go
if db_id('dbEMS') is not null
DROP DATABASE dbEMS
GO
create DATABASE dbEMS
RESTORE DATABASE dbEMS  
   FROM DISK = 'C:\Users\310171611\Desktop\142\database\dbEMS_A.bak'
   WITH MOVE 'dbEMS' TO 'C:\database\dbEMS.mdf', 

   MOVE 'dbEMS_log' TO 'C:\database\dbEMS_log.ldf', 
STATS = 10, REPLACE 
GO

declare @msg varchar(70)
if (@@ERROR <> 0 )
begin
   select @msg=convert(char(26),getdate(),9)+'-----dbEMS還原數據失敗或出現異常'
   print @msg
end
else
begin
   select @msg=convert(char(26),getdate(),9)+'-----dbEMS數據庫還原完畢'
   print @msg
END
-------------------------------------------------------------------------
USE MASTER 
go
if db_id('dbEMSConfig') is not null
DROP DATABASE dbEMSConfig
GO
create DATABASE dbEMSConfig
RESTORE DATABASE dbEMSConfig  
   FROM DISK = 'C:\Users\310171611\Desktop\142\database\dbEMSConfig_A.bak'
   WITH MOVE 'dbEMSConfig' TO 'C:\database\dbEMSConfig.mdf', 

   MOVE 'dbEMSConfig_log' TO 'C:\database\dbEMSConfig_log.ldf', 
STATS = 10, REPLACE 
GO

declare @msg varchar(70)
if (@@ERROR <> 0 )
begin
   select @msg=convert(char(26),getdate(),9)+'-----dbEMSConfig還原數據失敗或出現異常'
   print @msg
end
else
begin
   select @msg=convert(char(26),getdate(),9)+'-----dbEMSConfig數據庫還原完畢'
   print @msg
end

 

 

其餘的附加,分離的功能你們能夠根據本身的狀況來寫sql腳本。blog

相關文章
相關標籤/搜索