Solved

use  @@IDENTITY variable in a new query

Posted on 2014-02-17
5
246 Views
Last Modified: 2014-02-17
Hi all I have the code below
How can I use the value of the last insert ('F')
as an insert into a new table
CREATE TABLE AAMyTable (
  [GUID] [uniqueidentifier] ROWGUIDCOL  NOT NULL, 
  abc     char(1) NOT NULL
 )
 INSERT INTO AAMyTable (abc) VALUES ('D')
 INSERT INTO AAMyTable (abc) VALUES ('E')
 INSERT INTO AAMyTable (abc) VALUES ('F')
 PRINT  @@IDENTITY

Open in new window


Insert into BBMyTable (xyz)(@@IDENTITY) given that the xyz column data type is uniqueidentifier null

Thanks in Advance
0
Comment
Question by:ZURINET
[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
  • 3
  • 2
5 Comments
 
LVL 66

Expert Comment

by:Jim Horn
ID: 39864746
Declare an int variable, then after every single-row insert set the SCOPE_IDENTITY() to that variable.
CREATE TABLE #foo (id int identity(1,1), msg varchar(25), previous_id int)

Declare @id int 

INSERT INTO #foo (msg, previous_id) VALUES ('Hows', 0) 
SELECT @id = SCOPE_IDENTITY()

INSERT INTO #foo (msg, previous_id) VALUES ('it', @id) 
SELECT @id = SCOPE_IDENTITY()

INSERT INTO #foo (msg, previous_id) VALUES ('goin', @id) 
SELECT @id = SCOPE_IDENTITY()

INSERT INTO #foo (msg, previous_id) VALUES ('eh?', @id) 
SELECT @id = SCOPE_IDENTITY()

SELECT * FROM #foo

Open in new window

0
 

Author Comment

by:ZURINET
ID: 39864771
Hi Horn

Thanks for your input

I need to pass this value to a variable.
SELECT @id = SCOPE_IDENTITY()

@id  = uniqueidentifier
0
 
LVL 66

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 39864790
>  [GUID] [uniqueidentifier] ROWGUIDCOL  NOT NULL,
Based on the above I don't see a Default Value so that it knows to populate GUID on an insert.   Perhaps ... DEFAULT NEWID()

>Insert into BBMyTable (xyz)(@@IDENTITY) given that the xyz column data type is
@@IDENTITY and SCOPE_IDENTITY() return numeric(18,0), not GUIDs, so that's not going to work.
0
 

Author Comment

by:ZURINET
ID: 39864888
Hi Jim Horn

Thanks for your input..
I will just create a new GUID and reference it before the first insert
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 39864895
Thanks for the grade.  Good luck with your project.  -Jim
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

695 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