| Author |
Topic |
|
thj
Starting Member
8 Posts |
Posted - 2008-09-04 : 05:46:58
|
Hi.I've got this Users table thats being repopulated every night withusers from Active Directory. Before this occurs I want to update ahistory table with the current users in the Users table.I could do this easily writing a powershell script or something alike,but I would like to do it in a Stored Procedure. The problem is thatmy SQL knowledge is not so good yet.Theres around 4300 users in my Users table, and heres the SP:SELECT k.[Index] AS KontraktID, b.BrugerLogonNavn, b.BrugerNavn, b.BrugerTelefon, b.BrugerEmail, b.BrugerBeskrivelseINTO #BrugereFROM AD_Brugere AS bINNER JOIN AD_OUs ON b.AD_OU_ID = AD_OUs.AD_OU_IDLEFT OUTER JOIN NS_Kontrakter AS k ON AD_OUs.AD_OU_ID = k.AD_OU_ID INSERT INTO AD_HistoriskeBrugere (KontraktID, BrugerLogonNavn, BrugerNavn, BrugerTelefon, BrugerEmail, BrugerBeskrivelse)SELECT b.KontraktID, b.BrugerLogonNavn, b.BrugerNavn, b.BrugerTelefon, b.BrugerEmail, b.BrugerBeskrivelseFROM #Brugere bWHERE NOT EXISTS (SELECT BrugerLogonNavn FROM AD_HistoriskeBrugere hb WHERE b.BrugerLogonNavn = hb.BrugerLogonNavn) UPDATE AD_HistoriskeBrugereSET KontraktID = b.KontraktID, BrugerLogonNavn = b.BrugerLogonNavn, BrugerNavn = b.BrugerNavn, BrugerTelefon = b.BrugerTelefon, BrugerEmail = b.BrugerEmail, BrugerBeskrivelse = b.BrugerBeskrivelseFROM #Brugere bWHERE EXISTS (SELECT BrugerLogonNavn FROM AD_HistoriskeBrugere hb WHERE b.BrugerLogonNavn = hb.BrugerLogonNavn) DROP TABLE #Brugere Where table AD_Brugere = Usersand table AD_HistoriskeBrugere = UsersHistoryWhen I run this, it correctly inserts new users that does not exists in the UsersHistory table. The problem is the part that should Update the users that already exists in the UsersHistory table. Like I said, theres around 4300 users, and I stopped that UPDATE query after 30 minutes. Does it run in a loop or something? It can't take 30 minutes to update 4300 rows.I hope you can help me solve this problem.Thanks in advance. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-09-04 : 05:51:11
|
| you could just create a trigger on your Users table for delete,update to move the old values from users to your history table |
 |
|
|
thj
Starting Member
8 Posts |
Posted - 2008-09-04 : 05:52:25
|
quote: Originally posted by visakh16 you could just create a trigger on your Users table for delete,update to move the old values from users to your history table
The Users table is being truncated every night, so I don't think thats an option? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-09-04 : 05:53:02
|
something likeCREATE TRIGGER LogHistoryInfoON User AFTER UPDATE,DELETEASINSERT INTO User_History(reqdfields...)SELECT reqdfieldsFROM DELETED |
 |
|
|
thj
Starting Member
8 Posts |
Posted - 2008-09-04 : 05:54:56
|
quote: Originally posted by visakh16 something likeCREATE TRIGGER LogHistoryInfoON User AFTER UPDATE,DELETEASINSERT INTO User_History(reqdfields...)SELECT reqdfieldsFROM DELETED
I'm not deleting or updating users in the Users table. It's beeing truncated and repopulated from Active Directory every night. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-09-04 : 06:22:44
|
quote: Originally posted by thj
quote: Originally posted by visakh16 something likeCREATE TRIGGER LogHistoryInfoON User AFTER UPDATE,DELETEASINSERT INTO User_History(reqdfields...)SELECT reqdfieldsFROM DELETED
I'm not deleting or updating users in the Users table. It's beeing truncated and repopulated from Active Directory every night.
then what you could do is to write a procedure which just copies all the records from users table to user history along with current datevalue and execute this procedure just before you truncate your table. you could include all this inside a sql job or just use DTS package with all this steps and execute it via job. |
 |
|
|
thj
Starting Member
8 Posts |
Posted - 2008-09-04 : 06:47:32
|
quote: Originally posted by visakh16then what you could do is to write a procedure which just copies all the records from users table to user history along with current datevalue and execute this procedure just before you truncate your table. you could include all this inside a sql job or just use DTS package with all this steps and execute it via job.
Correct, and thats what I've been trying to do :-)Insert non existing records and update existing. But the update part doesn't work. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-09-04 : 06:58:58
|
quote: Originally posted by thj
quote: Originally posted by visakh16then what you could do is to write a procedure which just copies all the records from users table to user history along with current datevalue and execute this procedure just before you truncate your table. you could include all this inside a sql job or just use DTS package with all this steps and execute it via job.
Correct, and thats what I've been trying to do :-)Insert non existing records and update existing. But the update part doesn't work.
why is it not working. can you elaborate. may be with your update code? |
 |
|
|
thj
Starting Member
8 Posts |
Posted - 2008-09-04 : 07:02:07
|
quote: Originally posted by visakh16why is it not working. can you elaborate. may be with your update code?
I already explained this in the first post:"When I run this, it correctly inserts new users that does not exists in the UsersHistory table. The problem is the part that should Update the users that already exists in the UsersHistory table. Like I said, theres around 4300 users, and I stopped that UPDATE query after 30 minutes. Does it run in a loop or something? It can't take 30 minutes to update 4300 rows." |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-09-04 : 07:19:05
|
quote: Originally posted by thj
quote: Originally posted by visakh16why is it not working. can you elaborate. may be with your update code?
I already explained this in the first post:"When I run this, it correctly inserts new users that does not exists in the UsersHistory table. The problem is the part that should Update the users that already exists in the UsersHistory table. Like I said, theres around 4300 users, and I stopped that UPDATE query after 30 minutes. Does it run in a loop or something? It can't take 30 minutes to update 4300 rows."
have you tried using a join instead of exists?UPDATE AD_HistoriskeBrugereSET KontraktID = b.KontraktID, BrugerLogonNavn = b.BrugerLogonNavn, BrugerNavn = b.BrugerNavn, BrugerTelefon = b.BrugerTelefon, BrugerEmail = b.BrugerEmail, BrugerBeskrivelse = b.BrugerBeskrivelseFROM #Brugere bINNER JOIN AD_HistoriskeBrugere hbON b.BrugerLogonNavn = hb.BrugerLogonNavn |
 |
|
|
thj
Starting Member
8 Posts |
Posted - 2008-09-04 : 07:36:24
|
quote: Originally posted by visakh16have you tried using a join instead of exists?UPDATE AD_HistoriskeBrugereSET KontraktID = b.KontraktID, BrugerLogonNavn = b.BrugerLogonNavn, BrugerNavn = b.BrugerNavn, BrugerTelefon = b.BrugerTelefon, BrugerEmail = b.BrugerEmail, BrugerBeskrivelse = b.BrugerBeskrivelseFROM #Brugere bINNER JOIN AD_HistoriskeBrugere hbON b.BrugerLogonNavn = hb.BrugerLogonNavn
Aah, so simple :-)It's working. Thanks for your time man. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-09-04 : 07:40:50
|
quote: Originally posted by thj
quote: Originally posted by visakh16have you tried using a join instead of exists?UPDATE AD_HistoriskeBrugereSET KontraktID = b.KontraktID, BrugerLogonNavn = b.BrugerLogonNavn, BrugerNavn = b.BrugerNavn, BrugerTelefon = b.BrugerTelefon, BrugerEmail = b.BrugerEmail, BrugerBeskrivelse = b.BrugerBeskrivelseFROM #Brugere bINNER JOIN AD_HistoriskeBrugere hbON b.BrugerLogonNavn = hb.BrugerLogonNavn
Aah, so simple :-)It's working. Thanks for your time man.
you're welcome |
 |
|
|
|