Using NEWSEQUENTIALID () with UPDATE trigger

I am adding a new GUID / Uniqueidentifier column to my table.

ALTER TABLE table_name
ADD VersionNumber UNIQUEIDENTIFIER UNIQUE NOT NULL DEFAULT NEWSEQUENTIALID()
GO

And when ever a record is updated in a table, I would like to update this column "Version Number". So i create a new trigger

CREATE TRIGGER [DBO].[TR_TABLE_NAMWE]
ON [DBO].[TABLE_NAME]
AFTER UPDATE
AS 
BEGIN 
    UPDATE TABLE_NAME
    SET VERSIONNUMBER=NEWSEQUENTIALID()
    FROM TABLE_NAME D
    JOIN INSERTED I ON D.ID=I.ID/* some ID which is used to join*/
END
GO

But I just realized that NEWSEQUENTIALID () can only be used with CREATE TABLEor ALTER TABLE. I got this error

The newsequentialid() built-in function can only be used in a DEFAULT expression for a column of type 'uniqueidentifier' in a CREATE TABLE or ALTER TABLE statement. It cannot be combined with other operators to form a complex scalar expression.

Is there a workaround for this?

Edit1: changing NEWSEQUENTIALID()to NEWID()in the trigger allows this, but I will index this column and use NEWID()will be suboptimal

+3
source share
1 answer

, , - , :

DECLARE @T TABLE (G UNIQUEIDENTIFIER DEFAULT NEWSEQUENTIALID())
INSERT @T OUTPUT INSERTED.G VALUES (DEFAULT) 

GUID? rowversion, , , .

+9

Source: https://habr.com/ru/post/1744776/


All Articles