我想查詢一個數字列表到一個plsql變量中,並在另一個sql查詢中的in子句中使用它。我在下面創建了一個我想做的測試用例。在'IN'子句中使用綁定變量
我沒有谷歌的解決方案,我認爲這一定是可能的,但我只是沒有得到它運行。請幫助我編譯解決方案。使用
CREATE OR REPLACE PROCEDURE PROCEDURE1
as
type t_id is table of number;
v_ids t_id;
v_user_ids number;
BEGIN
-- fill variable v_id with id's, user_id is of type number
select user_id
bulk collect into v_ids
from user_users;
-- then at a later stage ... issue a query using v_id in the in clause
select user_id into v_user_ids from user_users
-- this line does not compile (local collection type not allowed in SQL statements)
where user_id in (v_ids);
END PROCEDURE1;
也許這是一個簡化的演示,但在你提供的情況下,你不只是將兩個查詢合併爲一個? 「Select ... from ... where user_id in(select user_id from ...)」?或者執行加入? –
是的,這是愚蠢的,可能沒有意義。我需要編譯的例子 - 我得到一個錯誤的行:其中user_id在(v_ids) – opi
正在使用一個SQL數組與plsql嵌套表不是你的選擇?如果是這樣,我們可以使用TABLE()函數來解決這個問題(儘管你的最後一個語句沒有意義,因爲它是一個select into,所以你最後只有一行?如果你真正的sql是select max ()或rownum = 1或其他)。 – DazzaL