sqlcmd

https://docs.microsoft.com/en-us/sql/relational-databases/scripting/sqlcmd-start-the-utility

C:\Users\clu>sqlcmd -?
Microsoft (R) SQL Server Command Line Tool
Version 11.0.2100.60 NT x64
Copyright (c) 2012 Microsoft. All rights reserved.

usage: Sqlcmd [-U login id] [-P password]
[-S server] [-H hostname] [-E trusted connection]
[-N Encrypt Connection][-C Trust Server Certificate]
[-d use database name] [-l login timeout] [-t query timeout]
[-h headers] [-s colseparator] [-w screen width]
[-a packetsize] [-e echo input] [-I Enable Quoted Identifiers]
[-c cmdend] [-L[c] list servers[clean output]]
[-q "cmdline query"] [-Q "cmdline query" and exit]
[-m errorlevel] [-V severitylevel] [-W remove trailing spaces]
[-u unicode output] [-r[0|1] msgs to stderr]
[-i inputfile] [-o outputfile] [-z new password]
[-f <codepage> | i:<codepage>[,o:<codepage>]] [-Z new password and exit]
[-k[1|2] remove[replace] control characters]
[-y variable length type display width]
[-Y fixed length type display width]
[-p[1] print statistics[colon format]]
[-R use client regional setting]
[-K application intent]
[-M multisubnet failover]
[-b On error batch abort]
[-v var = "value"...] [-A dedicated admin connection]
[-X[1] disable commands, startup script, environment variables [and exit]]
[-x disable variable substitution]
[-? show syntax summary]

 

https://stackoverflow.com/questions/2583517/run-all-sql-files-in-a-directory

Another approach would be to create a .BAT file with the following command:

for %%G in (*.sql) do sqlcmd /S servername /d databaseName -E -i"%%G"
pause

Place this .BAT file in the directory from which you want the .SQL files to be executed, double click the .BAT file and you are done!

 

参数说明

-S [protocol:]server[instance_name][,port]  
-d db_name  
-E (use trusted connection) 
-i input_file  
-U login_id  
-P password  


错误1

Sqlcmd: Error: Microsoft SQL Server Native Client 11.0 : Login failed. The login is from an untrusted domain and cannot be used with Windows authentication.. 

错误2

加上用户名和密码后提示

Sqlcmd: The -E and the -U/-P options are mutually exclusive.  互斥的

 修正

添加-U和-P,并且去掉-E就可以执行

输出到文件

 

 

脚本文件

http://www.cnblogs.com/softidea/p/4229829.html

(1).没有任何参数的for
格式为:for %%i in (set) do command
%%i 为变量
set为一个文件或者一组文件,其实就是一个集合。可用通配符,比如*.txt。
command 是要执行的命令。


看个例子: 当前目录中有如下文件: a.txt b.mp3 c.mp3 c.wma a.rm e.bat

你想显示里面的扩展名为mp3的文件,你会用什么命令呢?当然是dir了,dir *.mp3。 
同样的,for也可以来实现,如下: for %%i in (*.mp3) do echo %%i 这如何理解?
 for会先从括号里面执行,因为括号里面是*.mp3,所以for会先得到当前目录下所有的扩展名是mp3的文件,并把它们作为一个集合,而每个文件名就是一个元素,
像这样{b.mp3,c.mp3},然后用%%i依次代替每个元素,之后做do后面的命令。 当然,()里面不局限于一个文件或者通配符,可以有多个,比如: for %%i in (*.mp3,*.wma) do echo %%i 也是可以的。 注意:do 后面可以跟任何命令,不要局限于echo,这里只是演示。



>清空文件,再写入

>>追加

脚本的注释::


 

posted @ 2017-07-18 10:21  ChuckLu  阅读(456)  评论(0编辑  收藏  举报