BSMN
BSMN

Reputation: 5

SQL , INSERT with SELECT

I'm trying to INSERT new row with this values (hotelNo,guestNo,dataform,dataTo,roomNo) I know the hotel name , so I have to SELECT the hotelNo from another table , it didn't work with me , is there something wrong?

INSERT INTO Booking
VALUES (hotelNo,123,'3-sept-1014','3-sept-1014',121)
(SELECT hotelNo
 FROM Hotel
 WHERE hotelName='Ritz Carlton' AND city='Dubai');

Upvotes: 0

Views: 215

Answers (3)

Raging Bull
Raging Bull

Reputation: 18767

Remove VALUES (hotelNo,... from your query and you are good to go.

INSERT INTO Booking
(SELECT hotelNo,123,'3-sept-1014','3-sept-1014',121
 FROM Hotel
 WHERE hotelName='Ritz Carlton' AND city='Dubai')

Upvotes: 5

Andrew
Andrew

Reputation: 53

Try this:

INSERT INTO Booking VALUES (
(SELECT hotelNo
FROM Hotel
WHERE hotelName='Ritz Carlton' AND city='Dubai'),
123,'3-sept-1014','3-sept-1014',121);

Upvotes: 1

Jerko W. Tisler
Jerko W. Tisler

Reputation: 989

You should do it without VALUES

INSERT INTO Booking 
(SELECT hotelNo, 123, '3-sept-1014','3-sept-1014',121
 FROM Hotel
 WHERE hotelName='Ritz Carlton' AND city='Dubai');

Upvotes: 3

Related Questions