Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 New Unique Identifier

Author  Topic 

EugeneLim11
Posting Yak Master

167 Posts

Posted - 2008-06-25 : 02:19:15
Hi experts,

I got a a store procedure,

CREATE PROCEDURE PROCEDURENAME
@columnName varchar(50)
AS
BEGIN
Insert into TableName(columnName1, PrimaryColumnName)
values (@columnName, newID())
END

Now I have another table that use newID() as a foreign key. How do I retrieve the newID() value from the store procedure above so that I can use it as an input field in another table?

Your help is very much appreciated.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-25 : 02:22:12
Are you using SQL 2005?
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2008-06-25 : 02:23:34
[code]CREATE PROCEDURE PROCEDURENAME
@columnName varchar(50),
@PrimaryColumnName varchar(50) OUTPUT
AS
BEGIN
SELECT @PrimaryColumnName = NEWID()
INSERT INTO TableName(columnName1, PrimaryColumnName)
VALUES (@columnName, @PrimaryColumnName)
END[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

EugeneLim11
Posting Yak Master

167 Posts

Posted - 2008-06-25 : 02:26:30
Thank you Khtan. I did not thought of using variables to hold the new ID(). This is a novel way of solving the problem. :)

visakh16, I am using Visual Studio tied to MSSQL 2005 Database.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-25 : 02:28:21
quote:
Originally posted by EugeneLim11

Thank you Khtan. I did not thought of using variables to hold the new ID(). This is a novel way of solving the problem. :)

visakh16, I am using Visual Studio tied to MSSQL 2005 Database.


then you can even use this

CREATE PROCEDURE PROCEDURENAME
@columnName varchar(50)
AS
BEGIN
Insert into TableName(columnName1, PrimaryColumnName)
OUTPUT INSERTED.PrimaryColumnName INTO OtherTable
values (@columnName, newID())
END
Go to Top of Page
   

- Advertisement -