Solved

insert newsequentialid

Posted on 2009-05-18
7
385 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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 Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
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…

840 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