Answer the question
In order to leave comments, you need to log in
Is it possible to combine INSERT and SELECT in one question?
Good morning, Khabravchane!
Can you please tell me if it is possible to combine INSERT and SELECT in one query?
The task is as follows:
There is a label `texts`
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`text` longtext NOT NULL,
`link` bigint(20) NOT NULL,
`date` text NOT NULL,
`time` text NOT NULL, It is
necessary to insert values into the table, and then immediately pull out the id of the last added record,
Answer the question
In order to leave comments, you need to log in
Maybe the OUTPUT syntax will work for you.
For MySQL , LAST_INSERT_ID();
For MSSQL SCOPE_IDENTITY();
You can use the construct SET @lastID := LAST_INSERT_ID();
and then use @lastID in the right place. At least that's how my price list processor works.
INSERT INTO `my_table` SET
uid="Х-1234",
pic_small="/UserFiles/Image/Х-1234_1s.jpg",
pic_big="/UserFiles/Image/Х-1234_1.jpg",
name="BOSCH",
price="1200";
SET @lastID := LAST_INSERT_ID();
INSERT INTO `my_table_foto` SET
[email protected],
name="/UserFiles/Image/Х-1234_1.jpg";
I use SCOPE_IDENTITY() as pletinsky said. Those. INSERT goes first, then SELECT separated by a semicolon:
string strSQL = "INSERT INTO ....";
strSQL = strSQL + "; SELECT SCOPE_IDENTITY() AS NewID;";
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question