有没有一种有效的方法来避免SQL中固定值的UNION?

时间:2011-03-24 13:43:39

标签: sql oracle coding-style

例如使用Oracle:我想要一些临时表用于Merge:

MERGE INTO my_target_table
USING (
        WITH tbl1 AS (       SELECT 'a'   col1 FROM dual            -- <--- THIS
                       UNION SELECT 'foo' col1 FROM dual            -- <--- IS
                       UNION SELECT 'doh' col1 FROM dual            -- <--- CRAPPY
                     ),
             tbl2 AS (       SELECT 'b'   col2, 'c' col3 FROM dual  -- <--- THIS
                       UNION SELECT 'x'   col2, 's' col3 FROM dual  -- <--- ALSO
                     )
        SELECT col1, col2, col3 FROM tbl1 CROSS JOIN tbl2
      ) my_source_view
   ON (     my_target_table.col1 = my_source_view.col1
        AND my_target_table.col2 = my_source_view.col2
      )
 WHEN MATCHED THEN UPDATE
                      SET my_target_table.col3 = my_source_view.col3
 WHEN NOT MATCHED THEN INSERT(               col1,                col2,                col3)
                       VALUES(my_source_view.col1, my_source_view.col2, my_source_view.col3)
;

这里的SELECT UNION SELECT模式非常烦人,我发现它很可怕(重复,冗长)。

您是否有诀窍来形成类似的查询,无论是否特定于Oracle?

谢谢

4 个答案:

答案 0 :(得分:6)

贝努瓦,

您可以将此用于包含一列的表格:

SQL> with tbl1 as
  2  ( select column_value col1
  3      from table(sys.dbms_debug_vc2coll('a','foo','doh'))
  4  )
  5  select *
  6    from tbl1
  7  /

COL1
------------------------------------------------------------
a
foo
doh

3 rows selected.

对于多个列,您需要创建两种类型(或使用现有类型),如下所示:

SQL> create type ot is object
  2  ( col1 varchar2(1)
  3  , col2 varchar2(1)
  4  );
  5  /

Type created.

SQL> create type ntt is table of ot;
  2  /

Type created.

SQL> with tbl2 as
  2  ( select *
  3      from table(ntt(ot('b','c'),ot('x','s')))
  4  )
  5  select *
  6    from tbl2
  7  /

C C
- -
b c
x s

2 rows selected.

的问候,
罗布。

答案 1 :(得分:3)

在SQL Server 2008中,您可以使用VALUES子句。不确定这是否在Oracle中可用,但您确实要求所有技术。

语法示例

SELECT col1 FROM (VALUES ('a'),('foo'), ('doh')) T  (col1)

答案 2 :(得分:2)

有几种方法可以做到这一点(如上所述,这里还有一些)

所有这些都来自asktom问题: http://tkyte.blogspot.com/2006/06/varying-in-lists.htmlhttp://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID:210612357425

var mystring varchar2
begin
:mystring := trim('a,foo,doh,b,x');
end;
/


SELECT 

    substr(v.mystring, v.start_of_string, v.next_comma_location - v.start_of_string) "The String"
      FROM (SELECT comma_location + 1 start_of_string,
                   mystring,
                   nvl(lead(comma_location, 1) over(ORDER BY comma_location), mystring_length + 1) AS 
    next_comma_location
              FROM (SELECT :mystring mystring,
                           instr(:mystring, ',', LEVEL) comma_location,
                           length(:mystring) mystring_length
                      FROM dual
                    CONNECT BY LEVEL < length(:mystring))) v
     WHERE v.start_of_string < v.next_comma_location;

- 或  http://laurentschneider.com/wordpress/2007/12/predefined-collections.html

 select * from table(sys.odcivarchar2List('a','foo','doh','b','x'));




 COLUMN_VALUE                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
----------------
a                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
foo                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
doh                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
b                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
x 

您可以将这些视为表格

答案 3 :(得分:1)

嗯,我同意

    SELECT 'a'   col1 FROM dual 
    UNION SELECT 'foo' col1 FROM dual 
    UNION SELECT 'doh' col1 FROM dual

很糟糕。你同意吗

    SELECT 'a'   col1 FROM dual 
    UNION ALL SELECT 'foo' col1 FROM dual 
    UNION ALL SELECT 'doh' col1 FROM dual

不是吗?

修改

如果您不喜欢语法的某些部分,您可以尝试动态SQL(但要意识到这是您的个人偏好,所以尽量限制在较大的系统上进行)。

这是一个链接http://www.oracle-base.com/articles/misc/DynamicInLists.php