我試圖呼叫一個程序,但轉換引數,但即使執行轉換,接收到的引數仍然是舊型別。
我的程式:
CREATE PROCEDURE transfer_money(
user_origin_id int,
user_destination_id int,
amount dec
)
LANGUAGE sql
AS $delimiter$
DECLARE
wallet_origin_id integer,
wallet_destination_id integer;
BEGIN
SELECT wallet_id INTO wallet_origin_id
FROM users
WHERE id = user_origin_id;
SELECT wallet_id INTO wallet_destination_id
FROM users
WHERE id = user_destination_id;
UPDATE wallets
SET balance = balance - amount
WHERE id = wallet_origin_id;
UPDATE wallets
SET balance = balance amount
WHERE id = wallet_destination_id;
commit;
END;
$delimiter$
我的電話:
CALL transfer_money(1, 2, cast(100.00 as DECIMAL));
錯誤:
ERROR: procedure transfer_money(integer, integer, numeric) does not exist
LINE 1: CALL transfer_money(1, 2, cast(100.00 as DECIMAL));
^
HINT: No procedure matches the given name and argument types. You might need to add explicit type casts.
SQL state: 42883
Character: 6
uj5u.com熱心網友回復:
鑄造不是問題。您的呼叫甚至可以在沒有任何轉換的情況下作業,使用數字常量或無型別字串常量。
CALL transfer_money(1, 2, 100);
CALL transfer_money('1', '2', '100');
函式型別決議會處理它(除非您宣告了使呼叫不明確的多載函式)。看:
- 有沒有辦法在 Postgres 中禁用函式多載
問題是,您從未創建過該程式。我怎么知道?因為您顯示的是只會引發例外的語法廢話。它被宣告為LANGUAGE sql但使用 PL/pgSQL 元素。(SQL 函式沒有帶有BEGINand的塊結構END,它們僅由 SQL 命令組成。)
此外,需要分號的冒號。
考慮這個固定和簡化的版本:
CREATE OR REPLACE PROCEDURE transfer_money(_user_origin_id int
, _user_destination_id int
, _amount dec)
LANGUAGE sql AS
$proc$
UPDATE wallets w
SET balance = w.balance - _amount
FROM users u
WHERE u.id = _user_origin_id
AND w.id = u.wallet_id;
UPDATE wallets w
SET balance = w.balance _amount
FROM users u
WHERE u.id = _user_destination_id
AND w.id = u.wallet_id;
COMMIT;
$proc$;
順便說一句,在執行CALL您顯示的類似操作時(沒有嵌套在具有更多命令的顯式事務中),自動提交無論如何都會處理 COMMIT,并且不需要顯式的COMMIT;.
但也許比你展示的更多?
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/532674.html
