用户输入multple值赋给变量1批 [英] User input multple values assigned to 1 variable BATCH

查看:163
本文介绍了用户输入multple值赋给变量1批的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的批处理文件看起来像这样

MY BATCH FILE LOOKS LIKE THIS

ECHO OFF

SET /P colu= Enter column name:
SET /P sn= Enter ID (Press enter when finished): 

 if %sn%={ENTER}
  GOTO START

  :START
sqlcmd -U USER -P PWORD -S  SERVER -d DBNAME-i sqlSCRIPT.sql -o LOG.txt 
-v delete=colu d_id=sn

我希望用户能够在一个单一行中输入多个ID。这些ID的会从数据库中使用被删除的 sqlScript.sql 然而,这code未让我进入multple IDS。

I want the user to be able to enter multiple ids in a single line. Those ID's will be deleted from the DB using the sqlScript.sql however this code is not allowing me to enter multple ids.

这是在看我的1整个价值输入值。

It is looking at the values I enter as 1 whole value.

有关一个例子,如果我输入的 1,2,3 (作为单独的值),它认为这是1,2,3作为全1值。

For an example if I enter 1,2,3 (as separate values) it sees it as '1,2,3' as 1 whole value.

推荐答案

有在code几个问题:

There are several issues in your code:


  • 变量 colu SN 应在开始时被清除,以避免使用一些前值;

  • 如果子句没有任何意义,而且语法错了;

  • 要使用一个变量的值,你需要扩大他们像%colu%;

  • 要陆续使用一个值,使用循环;

  • 有一条线断在 SQLCMD 命令行的中间;

  • the variables colu and sn should be cleared at the beginning to avoid using some former value;
  • the if clause has no sense, and the syntax was wrong;
  • to use values of a variable, you need to expand them like %colu%;
  • to use one value after another, use a for loop;
  • there is a line-break in the middle of the sqlcmd command line;

以下应该做你想要什么:

The following should do what you want:

@ECHO OFF

SET colu=
SET sn=
SET /P colu= Enter column name:
SET /P sn= Enter ID (Press enter when finished): 

FOR %%I IN (%sn%) DO (
  sqlcmd -U USER -P PWORD -S SERVER -d DBNAME-i sqlSCRIPT.sql -o LOG.txt -v delete=%colu% d_id=%%I
)

这篇关于用户输入multple值赋给变量1批的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆