Select 按顺序插入且不同

Select insert with sequence and distinct

我正在尝试执行 select 插入,其中我需要使用序列和不同的子句。

我知道我们不能使用不同的序列,所以我在 FROM 中嵌套了一个子查询,如下所示:

INSERT INTO  SchemaName.XXSON_ST_B2B_CTR_PRICING
(SEQ_NO,SEEB_KEY,MESSAGE_ID,MESSAGE_TYPE,CREATE_DTIME,CHANGE_DTIME,STATUS)
   SELECT
          XXSON.XXSON_B2B_CTR_PRICING_SEQ.NEXTVAL   AS SEQ_NO,
         'SESS_NO'          AS SEEB_KEY,
          srp.ITEM          AS MESSAGE_ID,
         'MESSAGE_TYPE'     AS MESSAGE_TYPE,
          sysdate           AS CREATE_DTIME,
          sysdate           AS CHANGE_DTIME,
          'I'               AS STATUS
   FROM 
     (SELECT distinct srp.ITEM
      FROM  SchemaName.NB_PRICELIST_CTRL pctrl,
            SchemaName.NB_PRICELIST_SRP srp
      WHERE STATUS = 'W'
      AND SOURCE_TABLE = 'NB_PRICELIST_SRP');

但是,有了这个,我无法将 srp.ITEM 作为 MESSAGE_ID 插入,因为我有一个无效的标识符 (ORA-00904),不惊喜在这里。有什么解决方法可以让我正确插入吗?

要摆脱 ORA-00904: invalid identifier srp.ITEM,您必须将 alias 分配给 derived table,后者用作存储在内存中的正常 table。在块内分配的 FROM 子句中的别名在块外不可见。

INSERT INTO  SchemaName.XXSON_ST_B2B_CTR_PRICING
    (SEQ_NO,SEEB_KEY,MESSAGE_ID,MESSAGE_TYPE,CREATE_DTIME,CHANGE_DTIME,STATUS)
        SELECT
                XXSON.XXSON_B2B_CTR_PRICING_SEQ.NEXTVAL   AS SEQ_NO,
                'SESS_NO'          AS SEEB_KEY,
                alias.ITEM          AS MESSAGE_ID,
                'MESSAGE_TYPE'     AS MESSAGE_TYPE,
                sysdate           AS CREATE_DTIME,
                sysdate           AS CHANGE_DTIME,
                'I'               AS STATUS
        FROM 
        (SELECT distinct srp.ITEM
            FROM  SchemaName.NB_PRICELIST_CTRL pctrl,
                    SchemaName.NB_PRICELIST_SRP srp
            WHERE STATUS = 'W'
            AND SOURCE_TABLE = 'NB_PRICELIST_SRP') alias;

请尝试以下查询,这可能对您有所帮助。

INSERT INTO  SchemaName.XXSON_ST_B2B_CTR_PRICING
(SEQ_NO,SEEB_KEY,MESSAGE_ID,MESSAGE_TYPE,CREATE_DTIME,CHANGE_DTIME,STATUS)
   SELECT
          XXSON.XXSON_B2B_CTR_PRICING_SEQ.NEXTVAL   AS SEQ_NO,
         'SESS_NO'          AS SEEB_KEY,
          (SELECT distinct srp.ITEM FROM  SchemaName.NB_PRICELIST_CTRL pctrl, SchemaName.NB_PRICELIST_SRP srp WHERE STATUS = 'W' AND SOURCE_TABLE = 'NB_PRICELIST_SRP') AS MESSAGE_ID,
         'MESSAGE_TYPE'     AS MESSAGE_TYPE,
          sysdate           AS CREATE_DTIME,
          sysdate           AS CHANGE_DTIME,
          'I'               AS STATUS;

此致。

您提供的错误消息涉及缺失或无效的列。我相信错误消息的具体问题是

中的 srp 别名
srp.item as Message_ID

因为您没有为括号中的 2 个表的组合设置别名(您为其中的各个表设置了别名,但这不会执行)。一个简单的解决方法是从顶部语句中 'item' 的前面删除 'srp.'。

INSERT INTO  SchemaName.XXSON_ST_B2B_CTR_PRICING
(SEQ_NO,SEEB_KEY,MESSAGE_ID,MESSAGE_TYPE,CREATE_DTIME,CHANGE_DTIME,STATUS)
   SELECT
          XXSON.XXSON_B2B_CTR_PRICING_SEQ.NEXTVAL   AS SEQ_NO,
         'SESS_NO'          AS SEEB_KEY,
          ITEM          AS MESSAGE_ID,
         'MESSAGE_TYPE'     AS MESSAGE_TYPE,
          sysdate           AS CREATE_DTIME,
          sysdate           AS CHANGE_DTIME,
          'I'               AS STATUS
   FROM 
     (SELECT distinct srp.ITEM
      FROM SchemaName.NB_PRICELIST_CTRL pctrl,
        SchemaName.NB_PRICELIST_SRP srp
      WHERE STATUS = 'W'
      AND SOURCE_TABLE = 'NB_PRICELIST_SRP')

单独说明,我不太理解包含pctrl。