Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 若参数为空,则更改where子句_Sql_Oracle_Function - Fatal编程技术网

Sql 若参数为空,则更改where子句

Sql 若参数为空,则更改where子句,sql,oracle,function,Sql,Oracle,Function,我有一个Oracle函数,它有3个参数,并使用参数在多个并集在一起的select语句中设置where子句值。以下是伪代码: create or replace function fn_newfunction (IN_1_id in VARCHAR2, IN_2 in VARCHAR2, IN_3 in VARCHAR2) RETURN T_varchar_table AS v_tab T_varchar_table; begin select cast(multiset (

我有一个Oracle函数,它有3个参数,并使用参数在多个并集在一起的select语句中设置where子句值。以下是伪代码:

create or replace function fn_newfunction
(IN_1_id in VARCHAR2, IN_2 in VARCHAR2, IN_3 in VARCHAR2)

RETURN T_varchar_table AS
v_tab T_varchar_table;

begin

  select
  cast(multiset (
         --add users
         select * from table1 opt where opt.col2 = IN_2 and opt.col3 = IN_3 and opt.col1 = IN_1_id
         union
         ...
         <insert 10+ select statements here with same values>

  ) as T_varchar_table)     
  END
    into v_tab
    from dual;
  return v_tab;
end;
创建或替换函数fn\u新函数
(VARCHAR2中的IN_1_id,VARCHAR2中的IN_2,VARCHAR2中的IN_3)
将T_varchar_表返回为
v_tab T_varchar_table;
开始
选择
铸造(多组)(
--添加用户
从表1中选择*选项,其中opt.col2=IN_2,opt.col3=IN_3,opt.col1=IN_1
联盟
...
)as T_varchar_表)
结束
进入v_选项卡
来自双重;
返回v_选项卡;
结束;

出现了一个用例,可以将任何IN参数的空值传递到函数中,并让它在where子句中选择参数为空的任何值。例如,如果IN_1_id被传递一个空值,则第一个select语句中的where子句将显示opt.col1中的任何值(甚至null)的位置。我怎样才能做到这一点?谢谢大家!

我最常用的逻辑(虽然不是在Oracle中)如下所示。我把它写成伪的,只是因为正如我所提到的,我相信这个方法论的问题,而不是语法

逻辑
为什么有效 如果未通过
@参数1

   --This evaluates to TRUE for every single row, because the first condition has been met
  (@Parameter1 IS NULL OR MyTable.Parameter1 = @Parameter1)
   --The first condition will never be met, because @Parameter1 is NOT NULL.
   --The second condition will only be met for rows that match the parameter.
  (@Parameter1 IS NULL OR MyTable.Parameter1 = @Parameter1)
如果您确实通过了
@Parameter1

   --This evaluates to TRUE for every single row, because the first condition has been met
  (@Parameter1 IS NULL OR MyTable.Parameter1 = @Parameter1)
   --The first condition will never be met, because @Parameter1 is NOT NULL.
   --The second condition will only be met for rows that match the parameter.
  (@Parameter1 IS NULL OR MyTable.Parameter1 = @Parameter1)

使用此方法,您可以有条件地将字段添加到
WHERE
子句中。

我最常用的逻辑(尽管不是在Oracle中)如下所示。我把它写成伪的,只是因为正如我所提到的,我相信这个方法论的问题,而不是语法

逻辑
为什么有效 如果未通过
@参数1

   --This evaluates to TRUE for every single row, because the first condition has been met
  (@Parameter1 IS NULL OR MyTable.Parameter1 = @Parameter1)
   --The first condition will never be met, because @Parameter1 is NOT NULL.
   --The second condition will only be met for rows that match the parameter.
  (@Parameter1 IS NULL OR MyTable.Parameter1 = @Parameter1)
如果您确实通过了
@Parameter1

   --This evaluates to TRUE for every single row, because the first condition has been met
  (@Parameter1 IS NULL OR MyTable.Parameter1 = @Parameter1)
   --The first condition will never be met, because @Parameter1 is NOT NULL.
   --The second condition will only be met for rows that match the parameter.
  (@Parameter1 IS NULL OR MyTable.Parameter1 = @Parameter1)

使用此方法,您可以有条件地将字段添加到
WHERE
子句。

您可以使用动态sql来解决问题

if IN_1_id is not null then
lv_where := ' and opt.col1 =IN_1_id1 ';
else
lv_where :=' ';
end if;
 EXECUTE IMMEDIATE 'select * from table1 opt where opt.col2 = IN_2 and     opt.col3 = IN_3 ' ||lv_where ;

您可以使用动态sql来解决问题

if IN_1_id is not null then
lv_where := ' and opt.col1 =IN_1_id1 ';
else
lv_where :=' ';
end if;
 EXECUTE IMMEDIATE 'select * from table1 opt where opt.col2 = IN_2 and     opt.col3 = IN_3 ' ||lv_where ;

在Oracle中,用
@
作为变量前缀是无效的语法;除此之外,方法学是好的。正如我提到的,我用伪代码写的。我对甲骨文不是很熟悉,但我认为逻辑确实是最基本的部分。这对我所需要的非常有效-谢谢!在Oracle中,用
@
作为变量前缀是无效的语法;除此之外,方法学是好的。正如我提到的,我用伪代码写的。我对甲骨文不是很熟悉,但我认为逻辑确实是最基本的部分。这对我所需要的非常有效-谢谢!