2

(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的[] [執行請求,但它沒有任何意義,我...

回答

3

你所有的語法變種,以傳遞一個數組是正確

問題是與該函數內的表達。您可以使用ANY construct進行測試,如@Mureinik provided或許多其他語法變體。在任何情況下與EXISTS表達運行測試:

CREATE OR REPLACE FUNCTION test_me(id_list bigint[]) 
    RETURNS bool AS 
$func$ 
BEGIN 
    IF EXISTS (SELECT 1 FROM t1 WHERE id = ANY ($1)) THEN 
     RETURN true; 
    ELSE 
     RETURN false; 
    END IF; 
END 
$func$ LANGUAGE plpgsql STABLE; 

簡單變形

雖然你正在返回一個boolean的價值,它可以更簡單。這可能只是爲了演示,但作爲概念驗證:

CREATE OR REPLACE FUNCTION test_me(id_list bigint[]) 
    RETURNS bool AS 
$func$ 
SELECT EXISTS (SELECT 1 FROM t1 WHERE id = ANY ($1)) 
$func$ LANGUAGE sql STABLE; 

相同的結果。

+0

即使您的解決方案非常好,我想知道如果我不能將一個標量表達式列表傳遞給存儲過程,並因此直接使用它們與「IN」選項。在這裏(http://www.postgresql.org/docs/9.1/static/extend-type-system.html#AEN49791)他們似乎認爲標量被轉換爲一種數組「對於每個標量類型,相應的數組類型自動創建「。所以我想有一種方法可以從標量傳遞給ARRAY,反之亦然。 – Nitseg

+0

或者甚至直接將某個標量列表以某種方式傳遞給該過程。 – Nitseg

+0

@Nitseg:您可以使用'VARIADIC'參數(作爲最後一個參數)將數組作爲標量值的動態列表傳遞。有時這種調用很方便,但它仍然是該函數內的一個普通數組:http://stackoverflow.com/questions/19202832/pass-multiple-values-in-single-parameter/19204560#19204560。 –

2

檢查,如果一個項目是最簡單的方法數組是= ANY

CREATE OR REPLACE FUNCTION test_me(id_list BIGINT[]) 
RETURNS BOOLEAN AS 
$$ 
BEGIN 
    PERFORM * FROM T1 WHERE id = ANY ($1); 
    IF FOUND THEN 
    RETURN TRUE; 
    ELSE 
    RETURN FALSE; 
    END IF; 
END; 
$$ 
LANGUAGE 'plpgsql';