2
  INSERT INTO items (
    item_id, psuedo_row_flag, item_hierarchy_level, 'some other cols...'  
  )
  WITH s_item_group
  AS 
    (
      SELECT hier_level, 'some other cols...'   FROM ITEM_GROUPS   
    )
    SELECT MY_SQL.nextval, 'N', hier_level FROM s_item_group
    UNION ALL
    SELECT MY_SQL.nextval, 'Y', hier_level + 1 FROM s_item_group;

基本上我想插入两组行。一个表示实际行,另一个是伪行。伪行将 psuedo_row_flag 设置为“Y”,并且其层次结构级别为其对应的实际行的层次结构级别的 +1。

当我尝试此查询时,我收到错误“此处不允许序列号”

4

3 回答 3

2

只需将您的 UNION ALL 包装在另一个子查询中:

 INSERT INTO items
   (item_id,
    psuedo_row_flag,
    item_hierarchy_level)
   WITH s_item_group AS
    (SELECT hier_level
       FROM ITEM_GROUPS)
   select my_sql.nextval as item_id,
          psuedo_row_flag,
          hier_level
     from (SELECT 'N' psuedo_row_flag,
                  hier_level
             FROM s_item_group
           UNION ALL
           SELECT 'Y' psuedo_row_flag,
                  hier_level + 1
             FROM s_item_group)

顺便说一句,您可能需要修复 psuedo_row_flag 的拼写 :-)

于 2013-04-29T13:00:40.077 回答
2

您不能在UNION'ed 查询中使用序列。

尝试这个:

INSERT
INTO    items
WITH    s_items_group AS (...)
SELECT  my_sql.nextval, flag, hier_level
FROM    (
        SELECT  'N' flag, hier_level
        FROM    s_items_group
        UNION ALL
        SELECT  'Y' flag, hier_level + 1
        FROM    s_items_group
        )
于 2013-04-29T13:02:22.937 回答
0

对于这种情况,如果您想为查询投影的每一行插入两行,请查看使用多表插入。序列有点问题,但您可以使用触发器填充该列。

于 2013-04-29T13:22:53.287 回答