[英]Error passing an array to PL/pgSQL stored procedure
I have this procedure: 我有这个程序:
CREATE OR REPLACE FUNCTION get_saldo_conto(idUtente integer, idConto integer, categories int[], end_date date) RETURNS numeric(8,2) AS $$
DECLARE
row_transazione transazione%ROWTYPE;
saldoIniziale numeric(8,2);
totale numeric(8,2);
BEGIN
saldoIniziale = (SELECT saldo_iniziale FROM conto WHERE id = idConto AND id_utente = idUtente);
totale = 0;
FOR row_transazione IN SELECT *
FROM transazione
LEFT JOIN categoria ON id_categoria = categoria.id
WHERE id_conto = idConto
AND transazione.id_utente = idUtente
AND id_categoria = ANY (categories)
AND data <= end_date
LOOP
IF(row_transazione.tipo = 'entrata') THEN
totale = totale + row_transazione.importo;
ELSE
totale = totale - row_transazione.importo;
END IF;
END LOOP;
RETURN (saldoIniziale + totale) AS saldo_corrente;
END;
$$ LANGUAGE 'plpgsql';
When I call it with for example with 当我用例如
SELECT get_saldo_conto('1','19','{1,2,4,5,6}', '20/01/2015');
gives me an error 给我一个错误
ERROR: op ANY/ALL (array) requires array on right side
Am I doing something wrong passing the array? 我在传递数组时做错了吗? I tried also passing like '{1,2,4,5,6}'::int[] with no success.
我也尝试过像'{1,2,4,5,6}':: int []的传递,但没有成功。
CREATE TABLE transazione(
id SERIAL PRIMARY KEY,
tipo VARCHAR(7) NOT NULL CHECK(tipo IN('spesa', 'entrata')),
importo NUMERIC(8,2) NOT NULL,
descrizione VARCHAR(40),
data DATE DEFAULT CURRENT_TIMESTAMP,
id_conto INTEGER NOT NULL REFERENCES conto(id) ON UPDATE CASCADE ON DELETE CASCADE,
id_utente INTEGER NOT NULL REFERENCES utente(id) ON UPDATE CASCADE ON DELETE CASCADE,
id_categoria INTEGER REFERENCES categoria(id) ON UPDATE CASCADE ON DELETE SET NULL
);
You defined the row variable 您定义了行变量
row_transazione transazione%ROWTYPE;
But then you assign SELECT * FROM transazione LEFT JOIN categoriato
it, which obviously does not fit the type. 但是随后您将
SELECT * FROM transazione LEFT JOIN categoriato
分配给它,这显然不适合该类型。
The error message you display, however, does not make sense. 但是,显示的错误消息没有任何意义。 The only case of
ANY
/ ALL
in your code looks correct. 您的代码中唯一的
ANY
/ ALL
情况看起来是正确的。 Are you sure you are calling the function you think you are calling? 确定要调用您认为正在调用的函数吗? Investigate with:
调查:
SELECT n.nspname, p.proname
, pg_get_function_identity_arguments(p.oid) AS params
FROM pg_proc p
JOIN pg_namespace n ON n.oid = p.pronamespace
WHERE p.proname = 'get_saldo_conto';
.. to find all functions with the given name. ..查找具有给定名称的所有功能。 And
和
SHOW search_path;
.. to check if the search_path
leads to the right one. ..检查
search_path
是否指向正确的search_path
。
Your function would work like this: 您的函数将像这样工作:
CREATE OR REPLACE FUNCTION get_saldo_conto(_id_utente integer
, _id_conto integer
, _categories int[]
, _end_date date)
RETURNS numeric(8,2) AS
$func$
DECLARE
row_trans transazione;
saldoIniziale numeric(8,2) := (SELECT saldo_iniziale
FROM conto
WHERE id_utente = _id_utente
AND id = _id_conto);
totale numeric(8,2) := 0;
BEGIN
FOR row_trans IN
SELECT t.*
FROM transazione t
-- LEFT JOIN categoria ON id_categoria = categoria.id -- useless anyway
WHERE t.id_utente = _id_utente
AND t.id_conto = _id_conto
AND t.id_categoria = ANY (_categories)
AND data <= _end_date
LOOP
IF row_trans.tipo = 'entrata' THEN
totale := totale + row_trans.importo;
ELSE
totale := totale - row_trans.importo;
END IF;
END LOOP;
RETURN (saldoIniziale + totale); -- AS saldo_corrente -- no alias here!
END
$func$ LANGUAGE plpgsql;
But that's just to showcase the syntax. 但这只是为了展示语法。 The function is expensive nonsense .
该功能是昂贵的废话 。
Replace with a simple SELECT
: 用简单的
SELECT
替换:
SELECT COALESCE((
SELECT saldo_iniziale
FROM conto
WHERE id_utente = _id_utente
AND id = _id_conto), 0)
+ COALESCE((
SELECT sum(CASE WHEN tipo = 'entrata' THEN importo ELSE 0 END)
- sum(CASE WHEN tipo = 'spesa' THEN importo ELSE 0 END)
FROM transazione
WHERE id_conto = _id_conto
AND id_utente = _id_utente
AND id_categoria = ANY (_categories)
AND data <= _end_date), 0) AS saldo;
Assuming rows in conto
are unique on (id_utente,id)
. 在假设行
conto
是独特的(id_utente,id)
Depending on implementation details the best query can vary. 根据实施细节,最佳查询可能会有所不同。 I chose a variant that is safe against missing rows and NULL values.
我选择了一个可以避免丢失行和NULL值的变体。 Either way, a plain query should be much faster than a loop over all rows.
无论哪种方式,普通查询都应该比循环遍历所有行快得多。
You can wrap this into a function (SQL or plpgsql) if you want. 如果需要,可以将其包装为一个函数(SQL或plpgsql)。
Aside: 在旁边:
transazione.tipo
should rather be an enum
type - or even just "char"
or boolean
. transazione.tipo
应该是enum
类型-甚至只是"char"
或boolean
。 varchar(7)
is a waste for tag with two possible values. varchar(7)
对于带有两个可能值的标记是浪费的。
And
should really be data DATE DEFAULT CURRENT_TIMESTAMP
data DATE DEFAULT CURRENT_DATE
.
应该确实是data DATE DEFAULT CURRENT_TIMESTAMP
data DATE DEFAULT CURRENT_DATE
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.