How do I update existing record in CildTable with NEWID while creating new record in ParentTable with same GUID

Tags:
SQL Server
First, let's insert a record into ChildTable:
INSERT INTO ChildTable (DataCol3, DataCol4) VALUES ('Rec2-1','Rec2-2')
As indicated in your article, ChildKey PK populates automatically. When I try to create a new record in ParentTable that is linked to the record previously created in ChildTable I'm having trouble. The following does not work. Please advise and thanks in advance.
DECLARE @myNewID uniqueidentifier 
SELECT @myNewID = NEWID() 

UPDATE ChildTable 
SET ParentKey = @myNewID
WHERE DataCol3 = 'Rec2-1'

INSERT INTO ParentTable 
(ParentKey, DataCol1, DataCol2) 
VALUES (@myNEWID, 'New3','New31')
0

Answer Wiki

Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

Discuss This Question: 1  Reply

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.
  • ToddN2000
    Think. Parents come before children. Update parent file before the child.
    131,380 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

Thanks! We'll email you when relevant content is added and updated.

Following

Share this item with your network: