I have a database in place with a couple thousand promo codes, and I need something implemented where when a worker goes to get an ID it will check their ID if it is already assigned to a promo code, will return it, if there ID is not in the system will return the next promo code in the queue. This code I have now works each line individually but not as whole. I feel like its a simple error but I need a second pair of eyes looking onto it
IF EXISTS(select promo_code from Promo where worker_id='ABC123')
select promo_code from Promo where worker_id='ABC123'
ELSE
insert into Promo(worker_id) values('ABC123')
where Id=count(worker_id)+1
Sample Data
Id worker_id promo_code
0 ABC123 1234567890
1 1928374657
2 9184373462
So if ABC123 was passed it would return me 1234567890, but if I passed it DEF456, it would then see DEF456 is not in the table, insert it in the table at Id=1