Solved

use  @@IDENTITY variable in a new query

Posted on 2014-02-17
5
241 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
  • 3
  • 2
5 Comments
 
LVL 65

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 65

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 65

Expert Comment

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

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

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.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

912 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

25 Experts available now in Live!

Get 1:1 Help Now