获取 .BAT 文件或 SQLCMD 以提示用户输入

发布于 2024-11-19 08:53:43 字数 215 浏览 3 评论 0原文

我正在尝试运行以下查询,

sqlcmd -STEMP7 -E -devolive_base -w256
    -QEXIT("DECLARE @r int EXEC @r = usp_AddToObservationtbl $,$,$ SELECT @r")

我正在尝试获取 BAT 文件来向用户询问 3 个变量......任何人都可以帮助我吗?

I am attempting to run the following query

sqlcmd -STEMP7 -E -devolive_base -w256
    -QEXIT("DECLARE @r int EXEC @r = usp_AddToObservationtbl $,$,$ SELECT @r")

I am attempting to get the BAT file to ask the user for 3 variables.... can anyone help me ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

柠北森屋 2024-11-26 08:53:43

您可以使用 set /p 获取变量:

set /p var1=Enter variable 1
set /p var2=Enter variable 2
set /p var3=Enter variable 3

...然后在命令行中使用它们:

sqlcmd -STEMP7 -E -devolive_base -w256 -QEXIT("DECLARE @r int EXEC @r = usp_AddToObservationtbl %var1%,%var2%,%var3% SELECT @r")

请记住,这仍然会让您容易受到 SQL 注入攻击或有人输​​入破坏性内容。

You can use set /p to get variables:

set /p var1=Enter variable 1
set /p var2=Enter variable 2
set /p var3=Enter variable 3

...then use them in your command line:

sqlcmd -STEMP7 -E -devolive_base -w256 -QEXIT("DECLARE @r int EXEC @r = usp_AddToObservationtbl %var1%,%var2%,%var3% SELECT @r")

Bear in mind this still leaves you open to SQL Injection attacks or someone entering something destructive.

谢谢评论的人。

问题似乎是我的 SQLCMD 语法不正确,但它没有抛出错误,如果我用 osql 替换它,变量替换确实有效。所以这很好用

set /p var1= Enter Userid
set /p var2= Enter Casino Name  
set /p var3= Enter Screename 

osql -STEMP7 -E -devolive_base -w256 -QEXIT("DECLARE @r int EXEC @r = usp_AddToObservationtbl %var1%,%var2%,%var3% SELECT @r") -b -oc:\bat\observation.log
exit errorlevel

Thanks the comments people.

The problem appeared to be my syntax for SQLCMD is incorrect, but its not throwing an error, the variable substitution does work if I replace it with osql instead. So this works fine

set /p var1= Enter Userid
set /p var2= Enter Casino Name  
set /p var3= Enter Screename 

osql -STEMP7 -E -devolive_base -w256 -QEXIT("DECLARE @r int EXEC @r = usp_AddToObservationtbl %var1%,%var2%,%var3% SELECT @r") -b -oc:\bat\observation.log
exit errorlevel
遮了一弯 2024-11-26 08:53:43

这是一个简单的示例,解决了基本问题并删除了实际查询的细节。

我删除了更改 Windows 提示符的命令和关闭回显的命令,因为这些命令只是用于美化输出,而不是解决方案的一部分。我还在数据输入提示中使用了下划线,其中通常有一个空格,以便您可以看到它。我在 bat 文件末尾留下了一个暂停,这只是为了查看输出所必需的,以防您想自己尝试一下。

test.bat

set /p batchvariable=Enter a number:_
sqlcmd -v sqlvariable=%batchvariable% -i test.sql
pause

test.sql

SELECT $(sqlvariable) AS [result];

当您运行批处理文件时,这就是您得到的结果。

结果

实际上,您可能会对批处理变量和 SQL 变量使用相同的名称。上面它们是不同的,以说明什么是什么。

如果您需要这样做,您可能需要多个变量。您可以通过重复 sqlcmd-v sqlvariablename=%batchvariablename% 部分来处理此问题,例如

-v v1=%v1% -v v2=%v2%

test.bat

set /p item=Item : 
set /p cost=Cost : 
sqlcmd -v item="%item%" -v cost=%cost% -i test.sql

test.sql

SELECT '$(item)' AS [item], $(cost) AS [cost];

请注意,其中添加了引号第二个示例(对于字符串数据)未出现在第一个示例中(仅包含数字数据)。

您可能希望针对项目字段运行 Box of Chocolates 示例并检查行为。

另一个例子

Here is a simple example with the basic problem solved and specifics of your actual query removed.

I have removed a command to change the Windows prompt and a command to turn echo off, as these were just used to beautify the output, not part of the solution. I've also used an underscore in the data input prompt where you would normally have a space, so that you can see it. I have left a pause at the end of the bat file, which is only necessary to see the output, in case you want to try it out yourself.

test.bat

set /p batchvariable=Enter a number:_
sqlcmd -v sqlvariable=%batchvariable% -i test.sql
pause

test.sql

SELECT $(sqlvariable) AS [result];

When you run the batch file, this is what you get.

Result

In practice you are likely to use the same names for the batch variable and the SQL variable. They are different above to illustrate what's what.

If you need to do this, you may need more than one variable. You handle this by repeating the -v sqlvariablename=%batchvariablename% part of the sqlcmd, e.g.

-v v1=%v1% -v v2=%v2%

test.bat

set /p item=Item : 
set /p cost=Cost : 
sqlcmd -v item="%item%" -v cost=%cost% -i test.sql

test.sql

SELECT '$(item)' AS [item], $(cost) AS [cost];

Note that there are quotes added into this second example (for string data) which didn't appear in the first example (with numerical data only).

You might want to run the example with Box of Chocolates for the item field and check the behavior.

Another example

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文