?
Solved

use  @@IDENTITY variable in a new query

Posted on 2014-02-17
5
Medium Priority
?
248 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 2000 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

Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
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
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 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.
Suggested Courses

752 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