I'm trying to insert values into a table called Division
and it has a foreign key called id_dep
from the table Departement
. The problem is that I couldn't insert values into the Division
table because of the foreign key. I searched the internet and found that I can't insert values into a foreign key directly but through a Select
. I made some changes and my command looks like this:
string req = "INSERT INTO DIVISION VALUES(SELECT id_dep FROM Departement WHERE libelle='"+dep+"','" + id + "','" + lib + "')";
Now I'm getting a problem about syntax error. How to resolve that?
Use INSERT INTO...SELECT
syntax,
INSERT INTO DIVISION (colName, colName2, colName3)
SELECT id_dep, @id, @lib
FROM Departement
WHERE libelle = @dep
Always parameterized the query to avoid sql injection.
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.