Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Users history table

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 with
users from Active Directory. Before this occurs I want to update a
history 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 that
my 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.BrugerBeskrivelse
INTO #Brugere
FROM AD_Brugere AS b
INNER JOIN AD_OUs ON b.AD_OU_ID = AD_OUs.AD_OU_ID
LEFT 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.BrugerBeskrivelse
FROM #Brugere b
WHERE NOT EXISTS (SELECT BrugerLogonNavn
FROM AD_HistoriskeBrugere hb
WHERE b.BrugerLogonNavn = hb.BrugerLogonNavn)

UPDATE AD_HistoriskeBrugere
SET KontraktID = b.KontraktID, BrugerLogonNavn = b.BrugerLogonNavn,
BrugerNavn = b.BrugerNavn, BrugerTelefon = b.BrugerTelefon,
BrugerEmail = b.BrugerEmail, BrugerBeskrivelse = b.BrugerBeskrivelse
FROM #Brugere b
WHERE EXISTS (SELECT BrugerLogonNavn
FROM AD_HistoriskeBrugere hb
WHERE b.BrugerLogonNavn = hb.BrugerLogonNavn)

DROP TABLE #Brugere


Where table AD_Brugere = Users
and table AD_HistoriskeBrugere = UsersHistory

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.

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
Go to Top of Page

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?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-09-04 : 05:53:02
something like

CREATE TRIGGER LogHistoryInfo
ON User
AFTER UPDATE,DELETE
AS

INSERT INTO User_History(reqdfields...)
SELECT reqdfields
FROM DELETED
Go to Top of Page

thj
Starting Member

8 Posts

Posted - 2008-09-04 : 05:54:56
quote:
Originally posted by visakh16

something like
CREATE TRIGGER LogHistoryInfo
ON User
AFTER UPDATE,DELETE
AS

INSERT INTO User_History(reqdfields...)
SELECT reqdfields
FROM DELETED



I'm not deleting or updating users in the Users table. It's beeing truncated and repopulated from Active Directory every night.
Go to Top of Page

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 like
CREATE TRIGGER LogHistoryInfo
ON User
AFTER UPDATE,DELETE
AS

INSERT INTO User_History(reqdfields...)
SELECT reqdfields
FROM 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.
Go to Top of Page

thj
Starting Member

8 Posts

Posted - 2008-09-04 : 06:47:32
quote:
Originally posted by visakh16
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.


Correct, and thats what I've been trying to do :-)
Insert non existing records and update existing. But the update part doesn't work.
Go to Top of Page

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 visakh16
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.


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?
Go to Top of Page

thj
Starting Member

8 Posts

Posted - 2008-09-04 : 07:02:07
quote:
Originally posted by visakh16
why 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."
Go to Top of Page

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 visakh16
why 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_HistoriskeBrugere
SET KontraktID = b.KontraktID, BrugerLogonNavn = b.BrugerLogonNavn,
BrugerNavn = b.BrugerNavn, BrugerTelefon = b.BrugerTelefon,
BrugerEmail = b.BrugerEmail, BrugerBeskrivelse = b.BrugerBeskrivelse
FROM #Brugere b
INNER JOIN AD_HistoriskeBrugere hb
ON b.BrugerLogonNavn = hb.BrugerLogonNavn

Go to Top of Page

thj
Starting Member

8 Posts

Posted - 2008-09-04 : 07:36:24
quote:
Originally posted by visakh16
have you tried using a join instead of exists?
UPDATE AD_HistoriskeBrugere
SET KontraktID = b.KontraktID, BrugerLogonNavn = b.BrugerLogonNavn,
BrugerNavn = b.BrugerNavn, BrugerTelefon = b.BrugerTelefon,
BrugerEmail = b.BrugerEmail, BrugerBeskrivelse = b.BrugerBeskrivelse
FROM #Brugere b
INNER JOIN AD_HistoriskeBrugere hb
ON b.BrugerLogonNavn = hb.BrugerLogonNavn




Aah, so simple :-)
It's working. Thanks for your time man.
Go to Top of Page

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 visakh16
have you tried using a join instead of exists?
UPDATE AD_HistoriskeBrugere
SET KontraktID = b.KontraktID, BrugerLogonNavn = b.BrugerLogonNavn,
BrugerNavn = b.BrugerNavn, BrugerTelefon = b.BrugerTelefon,
BrugerEmail = b.BrugerEmail, BrugerBeskrivelse = b.BrugerBeskrivelse
FROM #Brugere b
INNER JOIN AD_HistoriskeBrugere hb
ON b.BrugerLogonNavn = hb.BrugerLogonNavn




Aah, so simple :-)
It's working. Thanks for your time man.


you're welcome
Go to Top of Page
   

- Advertisement -