Hi everyone,
I have two tables called users and formerpasswords. In users I have two fields: name which is primary key and password. In formerpasswords I have three fields: number which is primary key, name and password. I would like to create a trigger on users table so if I insert pairs of values in users table for name and password fields and after I update the value for password field where name = some of the inserts I have made in users table, trigger fires and inserts old value of name and password in formerpasswords table. How could I do that in SQL Server? I know that this can be done easily in MySQL using a before update trigger but in SQL Server that doesn't exist so I look for a way to do this.
Thank you so much in advance.
April 2, 2020 at 2:39 pm
You could use an after trigger and insert the values in the deleted pseudo table into your history table.
With SQL2016 and above I would be inclines to make the table versioned:
April 2, 2020 at 3:22 pm
In users I have two fields: name which is primary key and password.
You may have a much larger issue on your hands. Are the passwords in clear text or have they been one-way hashed or ???
--Jeff Moden
Change is inevitable... Change for the better is not.
April 3, 2020 at 1:42 pm
It was an example for a course to learn to create triggers not a real database nor real data, the datatype of password field was varchar but if I had to use it in the real world I would use varbinary datatype for passwords field and encrypt them by using the ENCRYPTBYPASSPHRASE function.
BWAAA-HAAA!!! Whew! I feel much better about what you're doing now. Thanks for the feedback, Albert.
--Jeff Moden
Change is inevitable... Change for the better is not.
April 3, 2020 at 3:10 pm
Ok, with that being said, you could home-grow a solution pretty easily but take a look at "System-Versioned Temporal Tables" in SQL Server. I think that might be what you're looking for. Here's the "starter" link for the subject.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply