如何将 select 语句的 IN 子句中的参数作为具有多个值的参数传递?

Posted

技术标签:

【中文标题】如何将 select 语句的 IN 子句中的参数作为具有多个值的参数传递?【英文标题】:How to pass arguments in IN clause of select statement as parameter having multiple values? 【发布时间】:2019-12-05 06:14:02 【问题描述】:

我正在 unix 中编写一个脚本,我正在尝试实现以下内容

1) 连接到数据库 2) 运行选择查询并在文件中获取结果以进行验证

现在我写了以下内容

#!/bin/bash

file="./database.properties"

if [ -f "$file" ]
then
    echo "$file found."
 . $file

  echo "User Id       = " $userName
  echo "user password = " $password
  echo "schema  = " $schema

  sqlplus -S $userName/$password@$schema
  set feedback off trimspool on 
spool workflow_details.txt;
SELECT WORKFLOW_NAME, START_TIME, END_TIME, (END_TIME-START_TIME)*24*60 as TIME_TAKEN
 FROM schema1.table1
 WHERE WORKFLOW_NAME IN ('argument1,argument2,argument3,argument4')
 AND WORKFLOW_RUN_ID IN (SELECT MAX(WORKFLOW_RUN_ID) FROM schema2.table3
 WHERE WORKFLOW_NAME IN ('argument1'));
spool off;
exit;

else
    echo "$file not found."
fi

要求是 iam 在 In 子句中使用的值,即 (argument1,argument2....etc.) 存在于文件中,并且应修改脚本,以便获取参数并将其通过逗号放置在 In 子句中分离。参数的数量是动态的。如何修改代码。

简而言之,我需要在运行时从具有参数 details 的文件中获取 IN 子句的参数。该文件看起来就像有一个由参数组成的单列。

【问题讨论】:

使用集合,然后将集合传递给IN 子句。见这里***.com/questions/16185277/… 【参考方案1】:

正如我的 cmets 中所述,您需要使用 Collection 来满足您的要求。请参阅下面的演示和内联说明。

PLSQL

-- Declare a Nested table of type Number. You can declare it of type of your argument1,argument2..

    Create or replace type var is table of number;
    /

    DECLARE
        v_var   var := var ();
        v_num number;
    BEGIN
        --Fetching rows to collection
        SELECT * BULK COLLECT INTO
            v_var
        FROM (
            SELECT 1 FROM dual
            UNION ALL
            SELECT 2 FROM dual
        );

        --Printing values of collection 
        FOR rec IN 1..v_var.count LOOP
            dbms_output.put_line(v_var(rec) );
        END LOOP;

        --Using in Where clause.       
        Select count(1)
        into v_num
        from dual where 1 Member of v_var; --<-- this is how you pass the collection of number in `IN` clause.

        dbms_output.put_line(v_num );
    END;

在您的情况下:UNIX 脚本

#!/bin/bash

#read from file and prepare the "in clause" --<--Put a loop to read through the file
in_clause=argument1,argument2  #--Prepare your `in_clause`


file="./database.properties"

if [ -f "$file" ]
then
    echo "$file found."
 . $file

  echo "User Id       = " $userName
  echo "user password = " $password
  echo "schema  = " $schema

  sqlplus -S $userName/$password@$schema
  set feedback off trimspool on 
  spool workflow_details.txt;

   SELECT workflow_name,
      start_time,
      end_time,
      ( end_time - start_time ) * 24 * 60 AS time_taken
    FROM schema1.table1
    WHERE workflow_name IN ($in_clause )      #<--Use in clause
          AND   workflow_run_id IN (SELECT MAX(workflow_run_id) FROM schema2.table3 WHERE workflow_name IN ( 'argument1' )
    );

spool off;
exit; 
else 
echo "$file not found." 
fi

PS:未测试

【讨论】:

以上是关于如何将 select 语句的 IN 子句中的参数作为具有多个值的参数传递?的主要内容,如果未能解决你的问题,请参考以下文章