Solved

insert newsequentialid

Posted on 2009-05-18
7
393 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
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Concat multiple records into one line 3 42
SQL Query help 3 42
return value based on substr 10 48
What Is an Error? 2 24
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
A company’s centralized system that manages user data, security, and distributed resources is often a focus of criminal attention. Active Directory (AD) is no exception. In truth, it’s even more likely to be targeted due to the number of companies …
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…

749 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