Don't do it like that! MySQL and MSSQL are multiuser systems, so there is no guarantee that your select will retrieve the ID of the record you just entered - there is nothing that stops a different user entering a record after you do, and before your select.
Instead, either use @@IDENTITY to retrieve the last automatically generated ID on teh current connection (you can use
LAST_INSERT_ID()[
^] as well, but @@IDENTITY works for MSSQL and MYSQL as well) or better, use GUID ID's and set them from your other code - that way you don't need to fetch anything once inserted.
INSERT INTO table1(col1, col2, col3) VALUES (val1, val2 val3)
INSERT INTO table2(foreignKeyCol, col1, col2) VALUES (@@IDENTITY, val1, val2)