1873

Prev Next

Issue: if SES Web is used to give permissions to an Admin user account, deleting that account could cause some stale records/relationships to remain in the database.



The following SQL script can show if such problematic records exist in the database (in a healthy database this script should not return any records):

Script 1:

SELECT * FROM Admins WHERE DBK_Index NOT IN (SELECT DBK_Index FROM tblDBKs)

Now, the existence of any such stale/orphaned records could cause two issues:

1 - If a user with the same name were to be created, it cannot be added to the admin group (it won't show up on the list to be added to the group).
The following script can solve this data relationship issue in the database:

Script 2:

SELECT DBK_Index, DBK_ID INTO #AdminsDBK FROM tblDBKs WHERE DBK_ID IN (SELECT AdminID FROM Admins WHERE DBK_Index is NULL or (DBK_Index NOT IN (SELECT DBK_Index FROM tblDBKs))) GROUP BY DBK_ID, DBK_Index HAVING COUNT(DBK_ID) = 1 UPDATE Admins SET Admins.DBK_Index = #AdminsDBK.DBK_Index FROM Admins LEFT JOIN #AdminsDBK ON Admins.AdminID = #AdminsDBK.DBK_ID WHERE Admins.DBK_Index is NULL or (Admins.DBK_Index NOT IN (SELECT DBK_Index FROM tblDBKs)) DROP TABLE #AdminsDBK

2 - A more urgent and important issue is that upgrading this kind of problematic database would fail with an error message like the one shown in the image attached to this article - Ctrl-click on the DB_upgrade_error.png attachment in the list of attachments to this article, below.

"Script 2" above can solve this problem, too.

NOTE: If "Script 2" can't solve the problem (meaning after running "Script 2", a re-run of "Script 1" still shows the existence of some records), ...
... then Script 3 below needs to be run in order to delete these stale records:

Script 3:

DELETE FROM Admins WHERE DBK_Index is NULL or (DBK_Index NOT IN (SELECT DBK_Index FROM tblDBKs))