How do I use Newid() inside a INSERT TRIGGER?
Posted on 2003-11-03
MS suggests that one should set the NEWID() function as a Default on the column. This method, however, allows one to populate one's own value, effectively overriding the default supplied.
I come from a Interbase background where one has a BEFORE and AFTER TRIGGER entry point. From a security perspective it is then easy to prevent anyone from trying to circumvent the database integrity and populate their own GUID data. All one has to do is to populate the GUID inside the AFTER TRIGGER on the INSERT entry point of the table and then to check that the GUID column does not change in the BEFORE TRIGGER on the UPDATE entry point.
My understanding is that the MS equivalent method would be to populate the NEWID() in the standard INSERT TRIGGER and then do the check inside the AFTER UPDATE TRIGGER.
I would like to know: 1) Is this solution the correct approach?
2) How do I get the NEWID() to supply a unique value for each row to be inserted? Remember that triggers are set based and not row based.