lundi 29 juin 2015

Adding counted rows to running number in UPDATE

I am trying to UPDATE usernames to our database, but I have problem with UQ.

In our schema we have corporate admin users and contacts in the same table. Every corporate has 1 admin user which where composed by taking 6 characters from their name and adding running number (if there were 6 character duplicates).

For example:

CompanyName: Test Company
UserName: testco-1

Running number with admin users (with the same 6 character start) varies from 1 to 15(ish).

Our contact table has column CorporateAdminId which is set to NULL with Admin users, but contacts are referred to admin user with this field. So the table has a relationship to itself.

NOTE: Before contacts did not have usernames Because software upgrades our contacts need to have username also. Usernames to contacts are created with the same rule (6 char+running number) and the first 6 characters are defined by AdminUserId reference (not the contacts own corporate name)

For example:

AdminUserId: GUID (Refers to Test Company)
CompanyName: Data miner
UserName: testco-2

My problem here is that how can I count how many usernames there are in the database at the moment that start f.e with 'testco' and add this number to the running number that I use to create contact usernames (so there will be no collissions with UQ)

Aucun commentaire:

Enregistrer un commentaire