Solved

Update SQL Table Row with GUID after Identity Insert Complete

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

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.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

630 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