You can use variables to store the results from two queries, and then use these values ββin an INSERT .
If you are using Microsoft SQL Server, then the following may work (but there may be superficial syntax errors since they have not been tested). Note that I assumed that the type of your columns is int .
DECLARE @snid int SET @snid = NULL Select @snid = sn_id FROM device.sn WHERE dname_id = 62 and sn_value = '123415' IF @snid IS NULL BEGIN PRINT 'id does not exist' END ELSE BEGIN DECLARE @maxid int SELECT @maxid = MAX(id) AS maxid FROM device.list INSERT INTO parts (sn_id,device_id) VALUES (@snid, @maxid) END
source share