oracle 如何在运行时从用户那里获取输入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12516930/
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
how to get input from user at runtime
提问by Ravindra Bagale
i want to take runtime input from user in oracle 10g pl/sql blocks(i.e. interactive communication with user), is it possible?
我想在 oracle 10g pl/sql 块中从用户那里获取运行时输入(即与用户的交互通信),这可能吗?
declare
x number;
begin
x=&x;
end
this code gives error as & can't be used in oracle 10g.
此代码给出错误,因为 & 不能在 oracle 10g 中使用。
回答by Nick Krasnov
To read the user input and store it in a variable, for later use, you can use SQL*Plus command ACCEPT
.
要读取用户输入并将其存储在变量中以供以后使用,您可以使用 SQL*Plus 命令ACCEPT
。
Accept <your variable> <variable type if needed [number|char|date]> prompt 'message'
example
例子
accept x number prompt 'Please enter something: '
And then you can use the x
variable in a PL/SQL block as follows:
然后您可以x
在 PL/SQL 块中使用该变量,如下所示:
declare
a number;
begin
a := &x;
end;
/
Working with a string example:
使用字符串示例:
accept x char prompt 'Please enter something: '
declare
a varchar2(10);
begin
a := '&x'; -- for a substitution variable of char data type
end; -- to be treated as a character string it needs
/ -- to be enclosed with single quotation marks
回答by Dulith De Costa
That is because you have used following line to assign the value which is wrong.
那是因为您使用了以下行来分配错误的值。
x=&x;
In PL/SQL assignment is done using following.
在 PL/SQL 分配中使用以下方法完成。
:=
:=
So your code should be like this.
所以你的代码应该是这样的。
declare
x number;
begin
x:=&x;
-- Below line will output the number you received as an input
dbms_output.put_line(x);
end;
/
回答by Parag Sali
declare
a number;
b number;
begin
a:= :a;
b:= :b;
if a>b then
dbms_output.put_line('Large number is '||a);
else
dbms_output.put_line('Large number is '||b);
end if;
end;
回答by jaskirat singh
`DECLARE
c_id customers.id%type := &c_id;
c_name customers.name%type;
c_add customers.address%type;
c_sal customers.salary%type;
a integer := &a`
Here c_id customers.id%type := &c_id;statement inputs the c_id with type already defined in the table and statement a integer := &ajust input integer in variable a.
这里c_id customers.id%type := &c_id; 语句输入已在表中定义的类型的 c_id 和语句a 整数 := &a只是在变量 a 中输入整数。
回答by All Videos
you can try this too And it will work:
你也可以试试这个它会起作用:
DECLARE
a NUMBER;
b NUMBER;
BEGIN
a :=: a; --this will take input from user
b :=: b;
DBMS_OUTPUT.PUT_LINE('a = '|| a);
DBMS_OUTPUT.PUT_LINE('b = '|| b);
END;
回答by Tanvir Patel
TRY THIS
尝试这个
declare
a number;
begin
a := :a;
dbms_output.put_line('Inputed Number is >> '|| a);
end;
/
OR
declare
a number;
begin
a := :x;
dbms_output.put_line('Inputed Number is >> '|| a);
end;
/
回答by kaushik
its very simple
它非常简单
just write:
写就好了:
//first create table named test....
//首先创建名为test的表....
create table test (name varchar2(10),age number(5));
//when you run the above code a table will be created....
//当您运行上述代码时,将创建一个表....
//now we have to insert a name & an age..
//现在我们必须插入一个名字和一个年龄..
Make sure age will be inserted via opening a form that seeks our help to enter the value in it
确保通过打开一个表格来插入年龄,寻求我们的帮助以在其中输入值
insert into test values('Deepak', :age);
//now run the above code and you'll get "1 row inserted" output...
//现在运行上面的代码,你会得到“1行插入”输出...
/now run the select query to see the output
/ 现在运行选择查询以查看输出
select * from test;
//that's all ..Now i think no one has any queries left over accepting a user data...
//这就是全部..现在我认为没有人对接受用户数据有任何疑问......