简体   繁体   English

使用bat文件调用sql脚本

[英]Call sql script using bat file

It is necessary to use the bat file to call the sql script and pass two variables into it, but sql complains about them.需要使用bat文件来调用sql脚本并向其中传递两个变量,但是sql却报错了。 Asks to declare scalars.要求声明标量。

Here is the bat file itself.这是 bat 文件本身。

@ECHO OFF
echo SET @arg1 := %2, @arg2 := %3; > commands
copy /b commands + %1 + %2 > nul
"C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\170\Tools\Binn\sqlcmd.exe" -S DESKTOP-OQ8JGR5 -U SA -P 123 -i %1

And sql script.和 sql 脚本。

USE MVA
SELECT sum(number*price2 - number*price1) AS pribil FROM sale,goods
WHERE id=goods AND datepart(month, date)=@arg1 AND shop=@ar2

you can create a stored procedure and call that via sqlcmd from the bat file.您可以创建一个存储过程并通过 bat 文件中的 sqlcmd 调用它。 for example:例如:

create procedure [SP_count_up]
(
@input int
)
as
begin
    select @input + 1
end
go

and call it in you bat file via并通过在你的 bat 文件中调用它

sqlcmd -Q "exec dbo.SP_count_up @input=3" -S <server> -d <database>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM