简体   繁体   中英

I am having this error ORA-00917: missing comma?

I am working to insert data from two table into one table but I am getting this error "SQL Error: ORA-00917: missing comma " but I don't know where is the issue: Any help are welcome thanks.

INSERT INTO NAME_PROFFESION(NAME_ID AS 'NAME ID' , PROFFESION_ID AS 'PROFFESIONID' )
SELECT NAME_ID, PROFFESION_ID 
FROM  NAME
INNER JOIN PROFFESION 
ON NAME.PROFFESION_ID = PROFFESION.PROFFESION_ID;

The error:

Error starting at line : 107 in command -
 INSERT INTO NAME_PROFFESION(NAME_ID AS 'NAME ID' , PROFFESION_ID AS 'PROFFESIONID' )
 SELECT NAME_ID, PROFFESION_ID 
 FROM  NAME
 INNER JOIN PROFFESION 
 ON NAME.PROFFESION_ID = PROFFESION.PROFFESION_ID
 Error at Command Line : 107 Column : 37
 Error report -
 SQL Error: ORA-00917: missing comma
 00917. 00000 -  "missing comma"
 *Cause:    
 *Action:

And if I remove the AS I will get that error:

  Error starting at line : 107 in command -
  INSERT INTO NAME_PROFFESION(NAME_ID , PROFFESION_ID )
  SELECT NAME_ID, PROFFESION_ID 
  FROM  NAME
  INNER JOIN PROFFESION 
  ON NAME.PROFFESION_ID = PROFFESION.PROFFESION_ID
  Error at Command Line : 108 Column : 17
  Error report -
 SQL Error: ORA-00918: column ambiguously defined
 00918. 00000 -  "column ambiguously defined"
 *Cause:    
*Action:

Consider:

INSERT INTO NAME_PROFFESION(NAME_ID, PROFFESION_ID)
SELECT N.NAME_ID, N.PROFFESION_ID 
FROM NAME N
INNER JOIN PROFFESION P ON P.PROFFESION_ID = N.PROFFESION_ID;

That is:

  • the columns list of INSERT does not take aliases, just column names

  • all columns should qualified (prefixed) with the table they belong to, to avoid ambiguity when the same column is available in more than one table - table aliases come handy for this

Side note: as it is, your query could also be written:

INSERT INTO NAME_PROFFESION(NAME_ID, PROFFESION_ID)
SELECT N.NAME_ID, N.PROFFESION_ID 
FROM NAME N
WHERE EXISTS (SELECT 1 FROM PROFFESION WHERE P.PROFFESION_ID = N.PROFFESION_ID);

Try without the aliases for the fields, also there is a double space in FROM NAME :

INSERT INTO NAME_PROFFESION(NAME_ID, PROFFESION_ID)
SELECT NAME_ID, PROFFESION_ID 
FROM NAME
INNER JOIN PROFFESION 
ON NAME.PROFFESION_ID = PROFFESION.PROFFESION_ID;

EDIT:

The ambiguity is because there is no alias for the tables. So, try:

INSERT INTO NAME_PROFFESION(NAME_ID, PROFFESION_ID)
SELECT n.NAME_ID, n.PROFFESION_ID 
FROM NAME n
INNER JOIN PROFFESION p
ON n.PROFFESION_ID = p.PROFFESION_ID;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM