Bat+sqlcmd Batch Run script

Source: Internet
Author: User
Tags goto

Hello, this bat script can help developers to execute batches of all SQL scripts in a folder by file name in the specified database. You don't have to endure the cumbersome PowerShell invoke-sqlcmd, which means more than one option at run time.

BAT file

@echo Off@rem ******** ******** General Batch for starting SQL ******** ******** @REM%1 is the name of SQL script File@re M 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 UNAM E=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%svr Name%-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_NAM E%-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%-L  60-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 @go To 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


Calling methods


Test.sql

SET NOCOUNT Ongoselect ' $ (p1) ' as P1, ' $ (p2) ' as P2

Bulk Call

The d:test directory has multiple SQL files running in batches.

Run



Bat+sqlcmd Batch Run script

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.