1:
IF NOT EXISTS (SELECT MainID FROM table1 WHERE device LIKE 'TEST') INSERT INTO table1 (device) VALUES ('TEST') SELECT SCOPE_IDENTITY() as MainID