0

我必须根据 4 个输入变量在存储过程中动态准备更新语句。

假设 take 、 和testtest1输入参数。test2test3

然后我必须准备像这样的查询,请帮助我构建这个:

update emp 
   set empid='1' 
 where test = test 
   and test1 = test1 
   and test2 = test2 
   and test3 = test3

假设如果 test 和 test1 值为 null 并且 test2 和 test3 值不为 null ,那么我可以准备如下更新语句

update emp 
    set empid='1' 
  where test is null       
    and test1 is null 
    and test2 = test2 
    and test3 = test3
4

3 回答 3

2

如果您在过程中传递的一个或多个参数发生变化,NULL您可以编写UPDATE如下语句。真的没有必要使用动态 SQL。在此示例中,过程的参数以 为前缀p_

update emp 
   set empid='1' 
 where (test  = p_test  or (test  is null and p_test  is null)) 
   and (test1 = p_test1 or (test1 is null and p_test1 is null))
   and (test2 = p_test2 or (test2 is null and p_test2 is null)) 
   and (test3 = p_test3 or (test3 is null and p_test3 is null))
于 2013-09-11T21:43:46.150 回答
0

根据您的输入参数创建 SQL 语句,
例如

 'SELECT '||'Colum_nmae from table' || where var1=var2 .....

然后使用执行立即执行此查询

于 2013-09-11T17:08:53.537 回答
0

您不能为变量赋予与列相同的名称,这会使 PL/SQL 感到困惑。所以让我们命名变量v_test, v_test1, ...

你可以写:

update emp 
       set empid='1'
where  (test  = v_test  OR v_test  is null)
and    (test1 = v_test1 OR v_test1 is null)
and ... 

或者更简单地使用 NVL:

update emp 
       set empid='1'
where  test  = nvl(v_test, test) 
and    test1 = nvl(v_test1, test1) 
and ...

NVL(A, B)isA除非A是 null 在这种情况下它是B

于 2013-09-11T21:01:36.217 回答