Solved

Update SQL Table Row with GUID after Identity Insert Complete

Posted on 2013-11-02
3
846 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Webinar: AWS Backup & DR

Join our upcoming webinar with experts from AWS, CloudBerry Lab, and the Town of Edgartown IT to discuss best practices for simplifying online backup management and cutting costs.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

730 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