I have one table (customerselection) that lists customer numbers and promo codes:
CustomerNumber | Promo
12345 | ABCDEF
54321 | BCDEFG
22334 | BCDEFG
54678 | BCDEFG
23454 | ABCDEF
And another table (certificates) that lists Certificates, Promos, and CustomerNumbers, but CustomerNumber is initially NULL:
Certificate | Promo | CustomerNumber
1111111111 | ABCDEF | NULL
2222222222 | BCDEFG | NULL
3333333333 | BCDEFG | NULL
4444444444 | ABCDEF | NULL
What I need to do is uniquely assign each Certificate in the second table to a customer in the first table. The promos need to match. Update the second table with a customer number from the first, only to one record, with a matching promo.
I've done this in the past with a VB program - loop over records in the first table, update the first free record for the matching promo in the second table. But that program takes too long to run (there are usually around 600,000 records).
Is there any way to do this right in SQL? Accomplish the task with an awesomely convoluted (or even awesomely simple) SQL query? (Or SQL Server functionality?)
UPDATE There are many certificates per each promo. And many customers per promo.
But each customer can be assigned to only one certificate.
UPDATE 2 Let's call the first table customerselection and the second table certificates.
customerselection is a selection of customers that we want to assign certificates to.
certificates is a pool of certificates that can be assigned.