I was able to achieve it as below . Problem with inserting and then selecting is it does not guarantee the right id because of parallel transactions.   So I had to do the below var sql4 =   "WITH upd AS (INSERT INTO SamTempOptout (sOldEmail,iBlId,iRId,iOptOut) values ('"+ vOldEmail + "'," + vblId  +...