(1)在條件存儲過程語法
=>CREATE TABLE T1(id BIGSERIAL PRIMARY KEY, name TEXT);
CREATE TABLE
(2)
=>INSERT INTO T1
(name) VALUES
('Robert'),
('Simone');
INSERT 0 2
(3)
SELECT * FROM T1;
id | name
----+--------
1 | Robert
2 | Simone
(2 rows)
(4)
CREATE OR REPLACE FUNCTION test_me(id_list BIGINT[])
RETURNS BOOLEAN AS
$$
BEGIN
PERFORM * FROM T1 WHERE id IN ($1);
IF FOUND THEN
RETURN TRUE;
ELSE
RETURN FALSE;
END IF;
END;
$$
LANGUAGE 'plpgsql';
CREATE FUNCTION
我概率lem在調用過程時。我無法在網絡上找到一個示例來顯示如何傳遞BIGINT類型的值列表(或整數,無論如何)。
我嘗試沒有成功,接下來(語法錯誤):
第一語法:
eway=> SELECT * FROM test_me('{1,2}'::BIGINT[]);
ERROR: operator does not exist: bigint = bigint[]
LINE 1: SELECT * FROM T1 WHERE id IN ($1)
^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
QUERY: SELECT * FROM T1 WHERE id IN ($1)
CONTEXT: PL/pgSQL function test_me(bigint[]) line 3 at PERFORM
二語法:
eway=> SELECT * FROM test_me('{1,2}');
ERROR: operator does not exist: bigint = bigint[]
LINE 1: SELECT * FROM T1 WHERE id IN ($1)
^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
QUERY: SELECT * FROM T1 WHERE id IN ($1)
CONTEXT: PL/pgSQL function test_me(bigint[]) line 3 at PERFORM
第三句法:
eway=> SELECT * FROM test_me(ARRAY [1,2]);
ERROR: operator does not exist: bigint = bigint[]
LINE 1: SELECT * FROM T1 WHERE id IN ($1)
^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
QUERY: SELECT * FROM T1 WHERE id IN ($1)
CONTEXT: PL/pgSQL function test_me(bigint[]) line 3 at PERFORM
關於工作語法的任何線索?
這就像解析器正試圖轉換一個BIGINT到BIGINT的[] [執行請求,但它沒有任何意義,我...
即使您的解決方案非常好,我想知道如果我不能將一個標量表達式列表傳遞給存儲過程,並因此直接使用它們與「IN」選項。在這裏(http://www.postgresql.org/docs/9.1/static/extend-type-system.html#AEN49791)他們似乎認爲標量被轉換爲一種數組「對於每個標量類型,相應的數組類型自動創建「。所以我想有一種方法可以從標量傳遞給ARRAY,反之亦然。 – Nitseg
或者甚至直接將某個標量列表以某種方式傳遞給該過程。 – Nitseg
@Nitseg:您可以使用'VARIADIC'參數(作爲最後一個參數)將數組作爲標量值的動態列表傳遞。有時這種調用很方便,但它仍然是該函數內的一個普通數組:http://stackoverflow.com/questions/19202832/pass-multiple-values-in-single-parameter/19204560#19204560。 –