I have a two tables (Users
, DeletedUsers
) and a trigger Users_Delete
. When a user record is deleted I want to store the id and the last rowversion
value (before it was deleted) in the DeletedUsers
table.
Because SQL does not allow multiple columns of type rowversion
on a table, how should I store the LastUserVersion
in the DeletedUsers
table? What datatype should I use?
Sample Code:
CREATE TABLE [dbo].[Users] (
[ID] [uniqueidentifier] NOT NULL,
[Name] [nvarchar](100) NOT NULL,
[Version] [rowversion] NOT NULL
)
CREATE TABLE [dbo].[DeletedUsers] (
[ID] [uniqueidentifier] NOT NULL,
[LastUserVersion] [rowversion] NOT NULL, -- ERROR
[Version] [rowversion] NOT NULL
)
CREATE TRIGGER [dbo].[Users_Delete] ON [dbo].[Users]
AFTER DELETE
AS
BEGIN
SET NOCOUNT ON
INSERT INTO [dbo].[DeletedUsers]
(
ObjectID,
LastUserVersion
)
SELECT
ObjectID,
Version
FROM DELETED
SET NOCOUNT OFF
END
How do I store a rowversion values in a table that already has a rowversion column?
According to MSDN:
A nonnullable rowversion column is semantically equivalent to a binary(8) column. A nullable rowversion column is semantically equivalent to a varbinary(8) column.
Because the definition of [rowversion]
column is [Version] [rowversion] NOT NULL
you could use BINARY(8)
.