Solved

use  @@IDENTITY variable in a new query

Posted on 2014-02-17
5
240 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
GRANT, REVOKE, DENY 4 20
Script to randomly create characters in MS SQL 6 18
Stored procedure 23 25
Restrict result set 1 0
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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.

747 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

12 Experts available now in Live!

Get 1:1 Help Now