Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax error: missing expression (ORA-00936)

I have 2 tables, Facilities and Services.

CREATE TABLE Facilities (
facility_id NUMBER(2) NOT NULL,
facility_name VARCHAR2(20) NOT NULL,
CONSTRAINT pk_facil_id PRIMARY KEY (facility_id)
);

CREATE TABLE Services (
service_id NUMBER(2) NOT NULL,
service_name VARCHAR(20) NOT NULL,
service_facility NUMBER(2) NOT NULL,
CONSTRAINT pk_serviceid PRIMARY KEY (service_id)
);

ALTER TABLE Services
add CONSTRAINT fk_servicefacility FOREIGN KEY(service_facility) 
REFERENCES Facilities(facility_id);

If I try entering records into the 'Services' table like so:

INSERT INTO Services (service_id, service_name, service_facility) 
SELECT 06, 'Rooms', 
(SELECT facility_id, FROM Facilities WHERE facility_name = 'Hotel') 
FROM Dual;

I get an error "missing expression" for 3 out of the 7 insert statements. What expression is missing?

like image 615
adohertyd Avatar asked May 03 '12 22:05

adohertyd


People also ask

What is invalid identifier in SQL?

Ora-00904 Error Message “Invalid Identifier” Error Ora-00904 means you are attempting to execute an SQL statement that is one of the following: The SQL statement includes an invalid column name. The SQL statement includes a column name which does not currently exist.

Is not a group by expression?

ORA-00979 “ Not a GROUP BY expression ” is an error issued by the Oracle database when the SELECT statement contains a column that is neither listed in GROUP BY nor aggregated. This error message can be confusing to beginners.


2 Answers

The SQL statement you posted has an extra comma. If you run the statement in SQL*Plus, it will throw the ORA-00936: missing expression and show you exactly where the error occurs

SQL> ed
Wrote file afiedt.buf

  1  INSERT INTO Services (service_id, service_name, service_facility)
  2  SELECT 06, 'Rooms',
  3  (SELECT facility_id, FROM Facilities WHERE facility_name = 'Boston')
  4* FROM Dual
SQL> /
(SELECT facility_id, FROM Facilities WHERE facility_name = 'Boston')
                     *
ERROR at line 3:
ORA-00936: missing expression

If you remove the comma, the statement works

SQL> ed
Wrote file afiedt.buf

  1  INSERT INTO Services (service_id, service_name, service_facility)
  2  SELECT 06, 'Rooms',
  3  (SELECT facility_id FROM Facilities WHERE facility_name = 'Boston')
  4* FROM Dual
SQL> /

1 row created.

Note, however, that I would generally prefer Stefan's syntax where you are selecting from Facilities rather than selecting from dual with a scalar subquery.

like image 67
Justin Cave Avatar answered Oct 05 '22 06:10

Justin Cave


Your insert statement should be:

INSERT INTO 
    Services 
    (
        service_id, 
        service_name, 
        service_facility
    ) 
SELECT 
    06, 
    'Rooms', 
    facility_id 
FROM 
    Facilities 
WHERE 
    facility_name = 'Hotel'

In it's current state the query i provided will add a service record for rooms for each facility with the name of Hotel.

And then add on a join to your 'Dual' table so that you get the correct number of inserts / are getting the correct facilities.

like image 36
Stefan H Avatar answered Oct 05 '22 04:10

Stefan H