Question : MSSQL Select and Insert with a return value

Using the code below works when no record exists in Table1 it correctly returns MainID but if the record already exists it returns a NULL.
1:
IF NOT EXISTS (SELECT MainID FROM table1 WHERE device LIKE 'TEST') INSERT INTO table1 (device) VALUES ('TEST') SELECT SCOPE_IDENTITY() as MainID

Answer : MSSQL Select and Insert with a return value

Random Solutions  
 
programming4us programming4us