我遇到了许多类似于上述标题的查询。但我的情况完全不同。我正在尝试为具有INSERT
查询的表运行SELECT
命令以填充列值。下面是我正在尝试执行的查询。
INSERT INTO ELEC_ORDER_HT (OMS_SEQ.NEXTVAL, CUST_ID, ORDER_DATE, SHIP_METHOD,
REC_CREN_TM, REC_HIST_CREN_TM AS '23-NOV-17 01.56.01.103000 AM', ORDER_ID)
SELECT ELEC_ORDER.CUST_ID AS CUST_ID, ELEC_ORDER.ORDER_DATE AS ORDER_DATE,
ELEC_ORDER.SHIP_METHOD AS SHIP_METHOD, ELEC_ORDER.REC_CREN_TM AS REC_CREN_TM,
ELEC_ORDER.ORDER_ID AS ORDER_ID
FROM ELEC_ORDER
WHERE ELEC_ORDER.CUST_ID = 'CSTMR10001'
AND ELEC_ORDER.ORDER_ID = 'ELORD10001';
ELEC_ORDER_HT
和ELEC_ORDER
表中的列数不同。执行上述查询时出现以下错误。
REC_CREN_TM, REC_HIST_CREN_TM AS '23-NOV-17 01.56.01.103000 AM', ORDER_ID)
*
ERROR at line 2:
ORA-00917: missing comma
如何解决此问题?
答案 0 :(得分:2)
INSERT INTO ELEC_ORDER_HT (
OMS_SEQ.NEXTVAL, -- This does not appear to be a column name
CUST_ID,
ORDER_DATE,
SHIP_METHOD,
REC_CREN_TM,
REC_HIST_CREN_TM AS '23-NOV-17 01.56.01.103000 AM', -- you cannot use aliases here
ORDER_ID
)
SELECT ELEC_ORDER.CUST_ID AS CUST_ID,
ELEC_ORDER.ORDER_DATE AS ORDER_DATE,
ELEC_ORDER.SHIP_METHOD AS SHIP_METHOD,
ELEC_ORDER.REC_CREN_TM AS REC_CREN_TM,
ELEC_ORDER.ORDER_ID AS ORDER_ID -- only 5 columns but 7 in the previous part
FROM ELEC_ORDER
WHERE ELEC_ORDER.CUST_ID = 'CSTMR10001'
AND ELEC_ORDER.ORDER_ID = 'ELORD10001';
无效。语法是:
INSERT INTO table_name ( column_name, column_name )
SELECT value, value FROM other_table
您似乎试图用常量/序列值替换列名,然后从SELECT
语句中省略这些值。
你想要这样的东西:
INSERT INTO ELEC_ORDER_HT (
OMS, -- or whatever the correct column name is
CUST_ID,
ORDER_DATE,
SHIP_METHOD,
REC_CREN_TM,
REC_HIST_CREN_TM,
ORDER_ID
)
SELECT OMS_SEQ.NEXTVAL,
CUST_ID,
ORDER_DATE,
SHIP_METHOD,
REC_CREN_TM,
TIMESTAMP '2017-11-23 01.56.01.103000', -- or SYSTIMESTAMP if you want current time
ORDER_ID
FROM ELEC_ORDER
WHERE CUST_ID = 'CSTMR10001'
AND ORDER_ID = 'ELORD10001';