oracle SQL*Plus 如何从提示中接受文本变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16674252/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SQL*Plus how to accept text variable from prompt?
提问by Adam Varhegyi
Im very beginner in psql and i have a question.
我是 psql 的初学者,我有一个问题。
Here is the code:
这是代码:
SET serveroutput ON
ACCEPT myVariable PROMPT "Input value: ";
BEGIN
dbms_output.put_line('My input variable is: '||&myVariable);
END;
Question is very simple: How can i pass text to my variable? If i input a number it is works correctly and i can read in the log my number, but if i pass a text like "mytext" instead of a number, i got an error:
问题很简单:如何将文本传递给我的变量?如果我输入一个数字,它可以正常工作并且我可以在日志中读取我的数字,但是如果我传递像“mytext”这样的文本而不是数字,我会收到一个错误:
old:BEGIN
dbms_output.put_line('My input variable is: '||&myVariable);
END;
new:BEGIN
dbms_output.put_line('My input variable is: '||mytext);
END;
Error starting at line 5 in command:
BEGIN
dbms_output.put_line('My input variable is: '||&myVariable);
END;
Error report:
ORA-06550: 2 sor, 50 oszlop:
PLS-00201: identifier 'MYTEXT' must be declared
ORA-06550: 2 sor, 3 oszlop:
PL/SQL: Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
回答by Matthew Strawbridge
You have to specify the data type as part of the ACCEPT statement. If none is given, it assumes a number.
您必须将数据类型指定为ACCEPT 语句的一部分。如果没有给出,它假定一个数字。
Try ACCEPT myVariable CHAR PROMPT 'Input value: ';
instead.
试试吧ACCEPT myVariable CHAR PROMPT 'Input value: ';
。
回答by Alex Poole
You have to enclose the character substitution variable in quotes when you use it, if it's a string, otherwise Oracle tries to interpret the valueas an object name. You can see that on the 'new' version (shown because you have set verify on
), and sexta13 alluded to that too. So you would do:
使用字符替换变量时,必须用引号将其括起来,如果它是字符串,否则 Oracle 会尝试将该值解释为对象名称。您可以在“新”版本(显示是因为您拥有set verify on
)上看到这一点,而sexta13 也提到了这一点。所以你会这样做:
dbms_output.put_line('My input variable is: '||'&myVariable');
But you don't need to concatenate the value in this case (whether it's a number or s string):
但是在这种情况下您不需要连接值(无论是数字还是 s 字符串):
dbms_output.put_line('My input variable is: &myVariable');
回答by sexta13
You don't have MYTEXT variable declared anywhere.
您没有在任何地方声明 MYTEXT 变量。
dbms_output.put_line('My input variable is: '||mytext); -- here is the error. It should be &myVariable.