Solved

Update SQL Table Row with GUID after Identity Insert Complete

Posted on 2013-11-02
3
829 Views
Last Modified: 2013-11-08
I need to insert a GUID value in each row after the row is inserted.
This needs to be done using a constraint of trigger to update immediately when the row insert is complete. Default NewID() as a Constraint does NOT work at the insert stage
The field must be set to NULL by default
This statement needs to be run when the table is created.
Pls advise..
CREATE TRIGGER XTM1_Trigger_UpdateGUID_XTM1Manufacturers ON [dbo].[XTM1Manufacturers] 
    AFTER INSERT 
AS  BEGIN   
    UPDATE [dbo].[XTM1Manufacturers] 
SET @ManufacturerGUID = NEWID() Where [dbo].[XTM1Manufacturers].ManufacturerID= @ManufacturerID  
END 
GO

Open in new window

0
Comment
Question by:XGIS
  • 2
3 Comments
 
LVL 8

Expert Comment

by:Andrei Fomitchev
ID: 39619499
CREATE TRIGGER XTM1_Trigger_UpdateGUID_XTM1Manufacturers ON [dbo].[XTM1Manufacturers]
    AFTER INSERT
AS  BEGIN  
    UPDATE [dbo].[XTM1Manufacturers]
SET @ManufacturerGUID = NEWID() Where [dbo].[XTM1Manufacturers].ManufacturerID IN (SELECT ManufacturerID FROM Inserted)
END
GO
0
 
LVL 7

Accepted Solution

by:
XGIS earned 0 total points
ID: 39619592
Hello fomand...unfortunately this code returns;  Must declare the scalar variable "@ManufacturerGUID".

it goes like this....

use MSSQLDatabaseName
go

create trigger mysqlautoguid
on dbo.TableName
after insert
as
begin
    update dbo.TableName
    set GUIDFieldName = newid()
      from Inserted
end
0
 
LVL 7

Author Closing Comment

by:XGIS
ID: 39632965
it works
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now