我想在oracle 10g pl/sql塊(即與用戶的交互通信)中從用戶處獲得運行時輸入,是否有可能?如何在運行時從用戶獲取輸入
declare
x number;
begin
x=&x;
end
此代碼給誤差作爲&不能在Oracle 10g中使用。
我想在oracle 10g pl/sql塊(即與用戶的交互通信)中從用戶處獲得運行時輸入,是否有可能?如何在運行時從用戶獲取輸入
declare
x number;
begin
x=&x;
end
此代碼給誤差作爲&不能在Oracle 10g中使用。
要讀取用戶輸入並將其存儲在一個變量中,以備後用,可以使用sqlplus命令ACCEPT
。
Accept <your variable> <variable type if needed [number|char|date]> prompt 'message'
例如
accept x number prompt 'Please enter something: '
然後你就可以按如下方式使用x
變量在PL/SQL塊:
declare
a number;
begin
a := &x;
end;
/
它很簡單
只是寫:
//首先創建一個名爲test表....
create table test (name varchar2(10),age number(5));
//當你運行上面的代碼表將被創建....
//現在我們要插入一個名字&一個時代..
確保年齡將通過開放尋求我們的幫助一種形式,它
insert into test values('Deepak', :age);
//現在運行上面的代碼,你會進入價值被插入得到 「1個插入行」輸出...
/現在運行SELECT查詢看到輸出
select * from test;
//這是所有 ..Now我認爲沒有人留下來接受任何疑問用戶數據...
請使用代碼格式或內聯代碼元素,使您的帖子更具可讀性。 –
試試這個
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;
/
那是因爲你已經使用以下行來分配這是錯誤的值。
x=&x;
在PL/SQL分配中使用以下內容完成。
:=
所以,你的代碼應該是這樣的。
declare
x number;
begin
x:=&x;
-- Below line will output the number you received as an input
dbms_output.put_line(x);
end;
/
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;
你能解釋這是如何工作的?請注意,這個問題是在5年前發佈的,有一個被接受的,投票得很好的答案,只有幾行,所以在這段時間之後發佈一個更長的方法應該需要對如何改進現有答案提供某種評論。 – fedorqui
:先生,我曾嘗試這個代碼,它提供了以下錯誤:ORA-00900:無效的SQL語句 –
先生是有可能採取直接輸入,無需使用SQLPLUS。 –
我在Oracle 10g數據庫快遞版上運行它。不在sqlplus上 –