Home > Database > Mysql Tutorial > body text

bat+sqlcmd批量运行脚本

WBOY
Release: 2016-06-07 16:09:50
Original
1760 people have browsed it

Hello,此BAT脚本可以帮助开发人员将某文件夹下所有SQL脚本按文件名依次在指定数据库中批量执行。不用忍受powershell invoke-sqlcmd 的笨重,在指运行时多一种选择。 bat文件 @echo off@REM ******** ******** General Batch for Starting SQL ******** *****

Hello,此BAT脚本可以帮助开发人员将某文件夹下所有SQL脚本按文件名依次在指定数据库中批量执行。不用忍受powershell invoke-sqlcmd 的笨重,在指运行时多一种选择。

bat文件

@echo off
@REM ******** ******** General Batch for Starting SQL ******** ******** 
@REM %1 is the name of SQL script file
@rem SET SQL_NAME=%1
@SET SQL_NAME=%1
@SHIFT /1
@REM SHIFT /1
@REM P1... is parameters of SQLCMD,P1=%1...
@rem c for count
@set /a c=0

@rem ******** ******** parameters for sql server login ******** ********  
@set svrName=SERVERNAME\INSTANCENAME
@set uname=your_name
@set upwd=your_password
@set db=YOURDBNAME

@set s= %1 %2 %3 %4 %5 %6 %7 %8 %9
@FOR %%A IN (%s%) DO @set /a c=c+1
@if %c% gtr 9  goto s9
@if %c%==8  goto s8
@if %c%==7  goto s7
@if %c%==6  goto s6
@if %c%==5  goto s5
@if %c%==4  goto s4
@if %c%==3  goto s3
@if %c%==2  goto s2
@if %c%==1  goto s1
@if %c%==0  goto s0 
:s9
@sqlcmd -S%svrName% -U%uname% -P%upwd% -d%db% -i%SQL_NAME% -l60 -v P1=%1 P2=%2  p3=%3 p4=%4 p5=%5 p6=%6 p=%7 p8=%8 p9=%9 
@goto end
:s8
@sqlcmd -S%svrName% -U%uname% -P%upwd% -d%db% -i%SQL_NAME% -l60 -v P1=%1 P2=%2  p3=%3 p4=%4 p5=%5 p6=%6 p=%7 p8=%8  
@goto end
:s7
@sqlcmd -S%svrName% -U%uname% -P%upwd% -d%db% -i%SQL_NAME% -l60 -v P1=%1 P2=%2  p3=%3 p4=%4 p5=%5 p6=%6 p=%7  
@goto end
:s6
@sqlcmd -S%svrName% -U%uname% -P%upwd% -d%db% -i%SQL_NAME% -l60 -v P1=%1 P2=%2  p3=%3 p4=%4 p5=%5 p6=%6  
@goto end
:s5
@sqlcmd -S%svrName% -U%uname% -P%upwd% -d%db% -i%SQL_NAME% -l60 -v P1=%1 P2=%2  p3=%3 p4=%4 p5=%5  
@goto end
:s4
@sqlcmd -S%svrName% -U%uname% -P%upwd% -d%db% -i%SQL_NAME% -l60 -v P1=%1 P2=%2  p3=%3 p4=%4  
@goto end
:s3
@sqlcmd -S%svrName% -U%uname% -P%upwd% -d%db% -i%SQL_NAME% -l60 -v P1=%1 P2=%2  p3=%3 
@goto end
:s2
@sqlcmd -S%svrName% -U%uname% -P%upwd% -d%db% -i%SQL_NAME% -l60 -v P1=%1 P2=%2  
@goto end
:s1
@sqlcmd -S%svrName% -U%uname% -P%upwd% -d%db% -i%SQL_NAME% -l60 -v P1=%1 
@goto end
:s0
@sqlcmd -S%svrName% -U%uname% -P%upwd% -d%db% -i%SQL_NAME% 
@goto end
:end 
@echo off
@set svrName=
@set uname=
@set upwd=
@set db=
@exit /b 0 
Copy after login
调用方法

\

test.sql

set nocount on
go
select '$(p1)' as p1 ,'$(p2)' as p2
Copy after login

批量调用

d:test 目录下有多个 sql文件,批量运行。

\

运行

\
 

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!