Riddick
Riddick

Reputation: 593

Insert statement Oracle

Here is my table

create table reservations (
    ResID int NOT NULL,
    GuestID int,
    HotelID int,
    Check_in DATE,
    Check_out DATE,
    RoomType varchar2(15),
    Price Dec(8,2),
    PRIMARY KEY (ResID),
    CONSTRAINT FK_GuestIDX FOREIGN KEY(GuestID) REFERENCES Guests(GuestID),
    CONSTRAINT FK_HotelID FOREIGN KEY(HotelID) REFERENCES Hotel(HotelID)
);

Table was created with no problems. Now Im trying to populate the table

Here is my insert statement

insert into reservations (1, 1, 2, '17-DEC-2018', '21-DEC-2018', 'Suite', 87.03);

and here is the error that I'm getting

ORA-00928: missing SELECT keyword

What could be the cause for this?

Upvotes: 0

Views: 77

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269773

Or missing values:

insert into reservations
    values (1, 1, 2, '17-DEC-2018', '21-DEC-2018', 'Suite', 87.03);

That said, I would recommend writing this as:

insert into reservations (ResID, GuestID, HotelID, Check_in, Check_out DATE, RoomType, Price)
    values (1, 1, 2, DATE '2018-12-17', DATE '2018-12-21', 'Suite', 87.03);

Note:

  • List the columns after the insert. This can really prevent hard-to-debug errors.
  • This uses the DATE keyword to introduce a date constant.

You can also write this using SELECT:

insert into reservations (ResID, GuestID, HotelID, Check_in, Check_out DATE, RoomType, Price)
    select 1, 1, 2, DATE '2018-12-17', DATE '2018-12-21', 'Suite', 87.03
    from dual;

Upvotes: 1

Related Questions