Wednesday, March 7, 2012

Returning uniqueidentifier after insert

I have a stored procedure that I use to insert data, and I want the sp to
return the newid created. I used to be able to do with select @.@.Identity
with integer fields as the identity field, but it returns nothing when using
uniqueidentifier as the type.
Thanks.Try CASTing it to VARCHAR(36). I.e.,
DECLARE @.myID UNIQUEIDENTIFIER
SET @.myID = NEWID()
SELECT CAST(@.myID AS VARCHAR(36))
"Eagle" <eagletender2001@.yahoo.com> wrote in message
news:eVOq5UFbFHA.3184@.TK2MSFTNGP15.phx.gbl...
>I have a stored procedure that I use to insert data, and I want the sp to
>return the newid created. I used to be able to do with select @.@.Identity
>with integer fields as the identity field, but it returns nothing when
>using uniqueidentifier as the type.
> Thanks.
>
>|||Eagle wrote:
> I have a stored procedure that I use to insert data, and I want the sp to
> return the newid created. I used to be able to do with select @.@.Identity
> with integer fields as the identity field, but it returns nothing when usi
ng
> uniqueidentifier as the type.
When I had the same problem I couldn't find a way to do this.
But the nature of guid allows you to generate it on the client side
and just insert into the table, thus no need to return as you
already have it.|||@.@.identity will return a null value if you are inserting into a table withou
t
any identity columns. As far as I know there is know @.@. server type to
return the last inserted uniqueidentifier. If you are inserting only one
record at a time in the stored procedure, have you considering loading the
UID into a variable, then returning the variable?:
declare @.id uniqueidentifier
set @.id = newid()
insert into table (uid_column, x, y, y) values(@.id, 'x', 'y', 'z')
return @.id
"Eagle" wrote:

> I have a stored procedure that I use to insert data, and I want the sp to
> return the newid created. I used to be able to do with select @.@.Identity
> with integer fields as the identity field, but it returns nothing when usi
ng
> uniqueidentifier as the type.
> Thanks.
>
>|||You can't return an uniqueidentifier with @.@.IDENTITY IF you need to use it
after the insert, you should set a variable using NEWID() before the insert:
CREATE PROCEDURE procName (..., @.id uniqueidentifier OUTPUT) AS
BEGIN
SET @.id = NEWID()
INSERT tableName (..., [id]) VALUES (..., @.id)
END
"Eagle" wrote:

> I have a stored procedure that I use to insert data, and I want the sp to
> return the newid created. I used to be able to do with select @.@.Identity
> with integer fields as the identity field, but it returns nothing when usi
ng
> uniqueidentifier as the type.
> Thanks.
>
>

No comments:

Post a Comment