Solved

Update SQL Table Row with GUID after Identity Insert Complete

Posted on 2013-11-02
3
866 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

Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

Question has a verified solution.

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

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
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.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

728 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