Can someone tell me why this doesnt work?
INSERT INTO Medical_History (pid, grafts, allergies, diseases, surgearies, treatment)
VALUES ((SELECT pid FROM Pet WHERE pet_name='Jack' AND cid=(SELECT cid FROM Customer WHERE last_name='Iwannidis' AND first_name='Giwrgos')),
'grafts', 'allergies', 'diseases', 'surgearies', treatments');
I get a syntax error:
unrecognized token "');"
Subqueries also can be used with INSERT statements. The INSERT statement uses the data returned from the subquery to insert into another table. The selected data in the subquery can be modified with any of the character, date or number functions.
Subqueries can be used with SELECT, UPDATE, INSERT, DELETE statements along with expression operator.
The select
nested in the values
statement looks wrong (and I'm not sure that all databases accept it). A more typical way to express this is:
INSERT INTO Medical_History (pid, grafts, allergies, diseases, surgearies, treatment)
SELECT pid, 'grafts', 'allergies', 'diseases', 'surgearies', 'treatments'
FROM Pet
WHERE pet_name='Jack' AND
cid=(SELECT cid
FROM Customer
WHERE last_name='Iwannidis' AND first_name='Giwrgos'
);
This is particularly important if the subquery returns more than one value. Then the query is likely to get an error.
I had a syntax error because I had forgotten the quotes ('
) on 'treatments'.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With