Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Return the value of a uniqueidentifier from SQL Stored Procedure

Status
Not open for further replies.

primagic

IS-IT--Management
Jul 24, 2008
476
GB
I have the following stored procedure which inserts a new registration.

I have a field in the registrations table called Guid and fieldtype uniqueidentifier.

I want to retrieve that value on insertion.

Stored procedure
Code:
USE [xforafrica]
GO
/****** Object:  StoredProcedure [dbo].[AddNewRegistration]    Script Date: 08/31/2012 22:03:11 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[AddNewRegistration]
(
@Firstname nvarchar(100),
@Surname nvarchar(100),
@Company nvarchar(100),
@Password nvarchar(10),
@EmailAddress nvarchar(max),
@Guid UNIQUEIDENTIFIER OUTPUT

)
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO dbo.Registrations(Firstname, Surname, Company, Password, EmailAddress  )
VALUES (@Firstname, @Surname, @Company, @Password, @EmailAddress)
END
 
In cases like this, I usually create the guid separately and then insert it to the table, like this:

Code:
USE [xforafrica]
GO
/****** Object:  StoredProcedure [dbo].[AddNewRegistration]    Script Date: 08/31/2012 22:03:11 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[AddNewRegistration]
(
@Firstname nvarchar(100),
@Surname nvarchar(100),
@Company nvarchar(100),
@Password nvarchar(10),
@EmailAddress nvarchar(max),
@Guid UNIQUEIDENTIFIER OUTPUT

)
AS
BEGIN
SET NOCOUNT ON;
[!]Set @Guid = NewId()[/!]
INSERT INTO dbo.Registrations([!]GUID, [/!]Firstname, Surname, Company, Password, EmailAddress  )
VALUES ([!]@Guid,[/!] @Firstname, @Surname, @Company, @Password, @EmailAddress)
END

I did not test this code, but I suspect it will work without any problems.

-George
Microsoft SQL Server MVP
My Blogs
SQLCop
twitter
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top