I want to use the serial of an insert directly in a next insert.
Based on Help with T-SQL script: Insert record, then use identity of that insert on another statement? I execute the following SQL query:
DECLARE @Id1 BIGINT
DECLARE @Id2 BIGINT
INSERT INTO doses (CPS, ground, total) VALUES (10, 10, 10)
SET @Id1 = SELECT SCOPE_IDENTITY()
INSERT INTO places (x, y, z, speed) VALUES (10, 10, 10, 10)
SET @Id2 = SELECT SCOPE_IDENTITY()
INSERT INTO measurements (time, place, note, dose, id_dataset) VALUES ('Test', @Id1, 'test', @Id2, 17)
This give me a syntax error and I can't find anything wrong with my DECLARE.
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DECLARE @Id1 BIGINT
DECLARE @Id2 BIGINT INSERT INTO doses (CPS, ground, total)' at line 1
It seems that DECLARE in MySQL can only be used in a BEGIN..END. http://dev.mysql.com/doc/refman/5.0/en/declare.html
Also SCOPE_IDENTITY is replace with LAST_INSERT_ID().
This is my new query:
BEGIN
DECLARE @Id1 BIGINT;
DECLARE @Id2 BIGINT;
INSERT INTO doses (CPS, ground, total) VALUES (10, 10, 10);
SET @Id1 = (SELECT LAST_INSERT_ID());
INSERT INTO places (x, y, z, speed) VALUES (10, 10, 10, 10);
SET @Id2 = (SELECT LAST_INSERT_ID());
INSERT INTO measurements (time, place, note, dose, id_dataset);
VALUES ('Test', @Id1, 'test', @Id2, 17);
END
But I still get the same error.