Solved

insert newsequentialid

Posted on 2009-05-18
7
354 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

757 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

19 Experts available now in Live!

Get 1:1 Help Now