-1

当我在 select 语句中运行下面的 insert 时,我得到ORA 00937了因为下面的查询无法处理APPLICATIONS表上的子选择之一。我不想硬编码那个值。有什么建议么?

提前致谢。

insert into CONFIGURATION_PARAMETER_VALUES
( ID
, NAME
, DESCRIPTION
, DATA_TYPE
, VALUE_STRING
, VALUE_INTEGER
, VALUE_DATE
, VALUE_FLOAT
, VALUE_TIMESTAMP
, APPLICATION_ID
, DELETED
) 
select NVL(MAX(ID),0)+1
       , 'Alert_Statuses_AllExceptNoStatus'
       , 'Suspicious'
       , 'String'
       , 'RBS_EIM_AL_008'
       , null
       , null
       , null
       , null
       , (select ID from APPLICATIONS where name = 'Rabobank v 1.0.0.0')
       , 'N'
  from CONFIGURATION_PARAMETER_VALUES
4

2 回答 2

1

如果还不算太晚,我建议实施 aSEQUENCE而不是计数。您可能不会得到严格的数字顺序(可能会有差距),但每次都会得到一个唯一的值:

CREATE SEQUENCE Config_Parm_Values_Seq START WITH <1 + your current max ID>;

另请注意,您INSERT现在的行为将如下所示:

  • 如果表中没有记录,则不会插入任何内容。
  • 如果表中有记录,则每次执行它都会使表中的行数增加一倍。

因此,即使您不使用该序列,我也会考虑使用“普通旧”INSERT而不是INSERT ... SELECT. 此示例使用以下序列:

insert into CONFIGURATION_PARAMETER_VALUES
( ID
, NAME
, DESCRIPTION
, DATA_TYPE
, VALUE_STRING
, VALUE_INTEGER
, VALUE_DATE
, VALUE_FLOAT
, VALUE_TIMESTAMP
, APPLICATION_ID
, DELETED
) VALUES (
       Config_Parm_Values_Seq.NEXTVAL -- Use seqname.nextval to get
                                      -- the next value from the sequence
       , 'Alert_Statuses_AllExceptNoStatus'
       , 'Suspicious'
       , 'String'
       , 'RBS_EIM_AL_008'
       , null
       , null
       , null
       , null
       , (select MAX(ID) from APPLICATIONS where name = 'Rabobank v 1.0.0.0')
       , 'N')
于 2013-04-10T13:30:04.100 回答
0

The problem is in your SELECT statement where you are using SELECT NVL(MAX(ID), 0) + 1. As you are using MAX function in the SELECT list, you must use a GROUP BY, which is not the solution here.

Use something like the following:

insert into CONFIGURATION_PARAMETER_VALUES
( ID
, NAME
, DESCRIPTION
, DATA_TYPE
, VALUE_STRING
, VALUE_INTEGER
, VALUE_DATE
, VALUE_FLOAT
, VALUE_TIMESTAMP
, APPLICATION_ID
, DELETED
) 
select (SELECT MAX(ID) FROM configuration_parameter_values) + 1
        -- above line instead of NVL(MAX(ID),0)+1
        -- You can also put NVL function around the subquery.
       , 'Alert_Statuses_AllExceptNoStatus'
       , 'Suspicious'
       , 'String'
       , 'RBS_EIM_AL_008'
       , null
       , null
       , null
       , null
       , (select ID from APPLICATIONS where name = 'Rabobank v 1.0.0.0')
       -- Warning: The above subquery can generate a TOO_MANY_ROWS exception.
       -- Use the solution in the other answer to avoid this.
       , 'N'
  from CONFIGURATION_PARAMETER_VALUES
于 2013-04-10T13:08:02.467 回答