嗨,我想將 select 陳述句的一個值存盤到一個變數中,然后在程序中使用該變數更新我的表,但出現錯誤。我仍然不知道它只回傳一列然后也存在以下錯誤。 精確獲取回傳超過請求的行數。
這是演示代碼的示例。任何人都可以給我一個替代方案,我可以在這里做些什么來使其作業,因為我有很多這樣的 plsql 陳述句來填充表列
create or replace procedure pcountry (country IN Varchar) is
var_date Date;
begin
select date into var_date from countrytable where country=country;
update newtable
set date=var_date
where country=country
commit;
end pcountry;
uj5u.com熱心網友回復:
您需要更改程序輸入引數的名稱,因為您的表中有一個同名的列。甲骨文訓釋你的where子句
where country = country為where 1 = 1這始終是真實的。所以它回傳更多行而不是一行。
create or replace procedure pcountry (country_in IN Varchar) is
var_date Date;
begin
select date into var_date from countrytable where country = country_in ;
update newtable
set date=var_date
where country= country_in
commit;
end pcountry;
uj5u.com熱心網友回復:
跳過select,切換到merge。
CREATE OR REPLACE PROCEDURE pcountry (par_country IN varchr2)
IS
BEGIN
MERGE INTO newtable n
USING countrytable c
ON (c.country = n.country)
WHEN MATCHED
THEN
UPDATE SET n.date_column = c.date_column
WHERE n.country = par_country;
END;
/
uj5u.com熱心網友回復:
不要使用與列相同的名稱命名 PL/SQL 變數/引數。SQL 引擎將country優先于外部 PL/SQL 范圍查找本地 SQL 范圍中的值,因此country=country(假設非NULL值)1=1與您將匹配所有行。
假設每個country都是唯一的,那么:
create or replace procedure pcountry (
v_country IN COUNTRY_TABLE.COUNTRY%TYPE
) is
var_date COUNTRY_TABLE."DATE"%TYPE;
begin
select "DATE"
into var_date
from countrytable
where country=v_country;
update newtable
set "DATE"=var_date
where country=v_country
end pcountry;
/
此外,DATEis 是保留字,不能將 is 用作未加引號的識別符號。
您可以使用以下命令將其組合成單個 SQL 陳述句MERGE:
CREATE PROCEDURE pcountry (
v_country IN COUNTRY_TABLE.COUNTRY%TYPE
) is
var_date COUNTRY_TABLE."DATE"%TYPE;
BEGIN
MERGE INTO newtable n
USING (SELECT *
FROM countrytable
WHERE country = v_country) c
ON (c.country = n.country)
WHEN MATCHED THEN
UPDATE
SET "DATE" = c."DATE";
END pcountry;
/
uj5u.com熱心網友回復:
正如其他答案中所解釋的那樣,這個問題是,在等號兩側表示不同事物country的條件下期望是沒有意義的。該名稱具有多個含義 - 然后應用一組規則來確定每次使用該名稱時要接受的含義。這通常是名稱存在的最窄背景關系(“范圍”);在這種情況下,該名稱存在于 SQL 陳述句中參考的表中,所以這就是存在的意義。country = countrycountrycountry
一種解決方案很簡單 - 為程序中使用的引數使用不同的名稱。這在其他答案中也有體現。
不過還有另一種解決方案。如果您的程序已經很長,它可能是首選,它使用了一個引數名稱,如country,現在您需要添加一些代碼,您需要在 SQL 陳述句中使用此名稱。隨處更改引數名稱將非常耗時。令人高興的是,PL/SQL 理解限定名。country(您在where子句中使用它的地方)是查詢中參考的表的列名。但是如果你pcountry.country在右邊寫,用程序名來限定變數名,就不會再出現混淆了。
... where country = pcountry.country
將獲得與該執行緒中其他建議答案相同的結果。右側是來自程序的引數或變數,而不是表中的列名。
請注意,您還可以限定左側:
... where countrytable.country = pcountry.country
也許這對未來的讀者會更清楚。
然而,這不是幫助:
... where countrytable.country = country
你明白為什么嗎?
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/367865.html
上一篇:計算一年中存在的記錄,直到其結束日期為止已發布的每一年
下一篇:按條件連接列(oracle)
