如何使用BAT文件运行脚本?

时间:2022-05-17 02:08:22

I would like to have a BAT file open a sql server script. Currently I have this code in the sql file:

我想有一个BAT文件打开一个sql server脚本。目前我在sql文件中有这个代码:

declare @path varchar(255), @mydb varchar(50)
SELECT @mydb = 'timeclockplus'
select @path = 'C:\Program Files\Microsoft SQL Server\MSSQL.2\MSSQL\Backup\' 
            + @mydb + '-' + convert(varchar(8),getdate(),112) + '.bak'
BACKUP DATABASE @mydb TO DISK = @path

How do I open this SQL file from a BAT file?

如何从BAT文件中打开此SQL文件?

I am currently trying to run it like this:

我目前正在尝试像这样运行它:

C:\Program Files\Microsoft SQL Server\80\Tools\Binn\osql -E 
   -S Sql server-hl7\timeclockplus timeclockplus.sql -oresults.txt

but OSQL does not exist in the BINN directory,

但是BIDN目录中不存在OSQL,

4 个解决方案

#1


16  

You should invoke the sqlcmd command-line tool from your batch file. Assuming your sql file is "backup.sql", the command line would be something like:

您应该从批处理文件中调用sqlcmd命令行工具。假设您的sql文件是“backup.sql”,命令行将类似于:

sqlcmd -E -S yoursqlinstance -i backup.sql

-E uses trusted connection, replace with -U and -P if you need to specify a SQL username and password. See also this article with examples.

-E使用可信连接,如果需要指定SQL用户名和密码,请使用-U和-P替换。另见本文的示例。

#2


4  

sqlcmd -S 127.0.0.1 /E -i MySqlScript.sql

Replace /E with /U and /P if you don't have trusted connection

如果没有可信连接,请将/ E替换为/ U和/ P.

#3


3  

If you want a much better answer, here it is:

如果你想要一个更好的答案,这里是:

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
:: batch file for sql query
SET FIELDVAL=Empty
SET DBNAME=MYDB
SET SQLSTRING=SELECT column_name(s)^
 FROM table_name1^
 INNER JOIN table_name2^
 ON table_name1.column_name=table_name2.column_name^
 AND table_name2.field=%FIELDVAL%
ECHO !SQLSTRING!

ECHO.
sqlcmd.exe -b -S localhost -E -d !DBNAME! -Q "!SQLSTRING!" -W
ECHO Query is done. Hit any key to close this window....
pause>nul

#4


0  

Start > Run > Type Cmd.

开始>运行>键入Cmd。

MyDrive:\Mypath\Mybat.bat

MyDrive:\ mypath中\ Mybat.bat

=)

=)

#1


16  

You should invoke the sqlcmd command-line tool from your batch file. Assuming your sql file is "backup.sql", the command line would be something like:

您应该从批处理文件中调用sqlcmd命令行工具。假设您的sql文件是“backup.sql”,命令行将类似于:

sqlcmd -E -S yoursqlinstance -i backup.sql

-E uses trusted connection, replace with -U and -P if you need to specify a SQL username and password. See also this article with examples.

-E使用可信连接,如果需要指定SQL用户名和密码,请使用-U和-P替换。另见本文的示例。

#2


4  

sqlcmd -S 127.0.0.1 /E -i MySqlScript.sql

Replace /E with /U and /P if you don't have trusted connection

如果没有可信连接,请将/ E替换为/ U和/ P.

#3


3  

If you want a much better answer, here it is:

如果你想要一个更好的答案,这里是:

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
:: batch file for sql query
SET FIELDVAL=Empty
SET DBNAME=MYDB
SET SQLSTRING=SELECT column_name(s)^
 FROM table_name1^
 INNER JOIN table_name2^
 ON table_name1.column_name=table_name2.column_name^
 AND table_name2.field=%FIELDVAL%
ECHO !SQLSTRING!

ECHO.
sqlcmd.exe -b -S localhost -E -d !DBNAME! -Q "!SQLSTRING!" -W
ECHO Query is done. Hit any key to close this window....
pause>nul

#4


0  

Start > Run > Type Cmd.

开始>运行>键入Cmd。

MyDrive:\Mypath\Mybat.bat

MyDrive:\ mypath中\ Mybat.bat

=)

=)