如何在存储过程中创建字符串列表变量,以及如何在另一个查询中使用它?

时间:2019-03-24 06:59:05

标签: sql oracle stored-procedures plsql

我想在变量中保存一个字符串列表,而我又想在另一个查询中使用它。

这是我的存储过程:

CREATE PROCEDURE store_validated_sku_id
    (variable_sku_id OUT VARCHAR2)
AS
BEGIN
    SELECT vsku.SKU_ID INTO variable_sku_id 
    FROM CHELSEA_prdcataloga.vs_sku_discont_details_test discontd
    JOIN CHELSEA_prdcataloga.vsx_dcs_sku vsku ON discontd.SWAP_SKU = vsku.JDA_SKU_ID 
    JOIN CHELSEA_prdcataloga.auto_ship_view bcc ON bcc.sku_id = vsku.SKU_ID 
    WHERE vsku.web_eligible = 1 
      AND vsku.discontinued = 0 
      AND bcc.auto_ship_eligible = 1;

    dbms_output.put_line('variable_sku_id = '|| variable_sku_id);
END store_validated_sku_id;

我希望variable_sku_id作为字符串列表,并希望在其他查询中使用它。

1 个答案:

答案 0 :(得分:0)

create procedure store_validated_sku_id(variable_sku_id OUT VARCHAR2)
 as
BEGIN
  SELECT listagg(vsku.sku_id, ',') within group(order by vsku.sku_id)
    INTO variable_sku_id
    FROM chelsea_prdcataloga.vs_sku_discont_details_test discontd
    JOIN chelsea_prdcataloga.vsx_dcs_sku vsku ON discontd.swap_sku = vsku.jda_sku_id
    JOIN chelsea_prdcataloga.auto_ship_view bcc ON bcc.sku_id = vsku.sku_id
   WHERE vsku.web_eligible = 1        
     AND vsku.discontinued = 0
     AND bcc.auto_ship_eligible = 1;

  dbms_output.put_line('variable_sku_id = ' || variable_sku_id);

END store_validated_sku_id;