Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

insert newsequentialid

Posted on 2009-05-18
7
Medium Priority
?
441 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
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
What we learned in Webroot's webinar on multi-vector protection.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

877 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