Solved

insert newsequentialid

Posted on 2009-05-18
7
411 Views
Last Modified: 2012-05-07
I have a table dbo.Profile with column
ProfileID - type uniqueidentifier, primary key generated by newsequentialid ()

I have to insert porifleID in another table dbo.Service (a foreign key relation)
If it was an identity field, i know scope_identity() or ident_current() works.

But how do I obtain and insert the last inserted uniqueidentifier (ProfileID) from Profile table into Service table?
0
Comment
Question by:Angel02
[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
  • 4
  • 2
7 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24415857
why dont you generate the id before you perform the first insert

declare @x uniqueidentifier
select @x  = newsequentialID()
--now do the inserts
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24415888
sorry that wont work , just use the output feature of sql server 2005

create table #tab  (x uniqueidentifier )
insert into urTable
output inserted.columnName into #Temp
SELECT NewsequentialID()

0
 

Author Comment

by:Angel02
ID: 24416092
Thank you for the reply. I knew I had to use output, but I coudn't frame it, neitehr could I from your code. Can you please provide more detail? For example, below is the query

INSERT INTO dbo.[User] (Name, Phone) VALUES ("xyz", "123")
-- This generates the uniqueidentifier for UserID column using newsequentialid ()

Then to Insert into the Service table
--ServiceID is again autogenerated using newsequentialID.

INSERT INTO dbo.Service (UserID) VALUES (??)
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 500 total points
ID: 24416156
declare @tab table (userid uniqueidentifier)
INSERT INTO dbo.[User] (Name, Phone)
output inserted.userid into @tab
SELECT 'xyz', '12'

declare @userid uniqueidentifier
select @userid = userid
from @tab
select @userid

if you are using sql server 2005, you need to use a temp table instead of table variable to hold the value of the OUTPUT
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 24417820
>>if you are using sql server 2005, you need to use a temp table instead of table variable to hold the value of the OUTPUT<<

Aneesh,

You may want to double check that.

Anthony
0
 

Author Comment

by:Angel02
ID: 24422420
Thanks Aneesh. This works great !

Also, in reagrd to what Anthony said, your code works good enough in SQL 2005 too, which is what I am using.

Thanks all !
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24422602
Thanks Anthony :)
0

Featured Post

Veeam gives away 10 full conference passes

Veeam is a VMworld 2017 US & Europe Platinum Sponsor. Enter the raffle to get the full conference pass. Pass includes the admission to all general and breakout sessions, VMware Hands-On Labs, Solutions Exchange, exclusive giveaways and the great VMworld Customer Appreciation Part

Question has a verified solution.

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

As technology users and professionals, we’re always learning. Our universal interest in advancing our knowledge of the trade is unmatched by most industries. It’s a curiosity that makes sense, given the climate of change. Within that, there lies a…
Lotus Notes has been used since a very long time as an e-mail client and is very popular because of it's unmatched security. In this article we are going to learn about  RRV Bucket corruption and understand various methods to Fix "RRV Bucket Corrupt…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

635 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