Solved

Update SQL Table Row with GUID after Identity Insert Complete

Posted on 2013-11-02
3
807 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
Comment Utility
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
Comment Utility
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
Comment Utility
it works
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In this article I will describe the Backup & Restore 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.
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 setup several different housekeeping processes for a SQL Server.

771 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

14 Experts available now in Live!

Get 1:1 Help Now