user592704
user592704

Reputation: 3704

SQL Server 2005 - T-SQL - how to get GUID as scalar function value

I tried to write such kind of code as

CREATE PROCEDURE DBO.GENCODE
AS
BEGIN

SELECT NEWID() AS GUID

END
GO

The thing is this procedure doesn't return a var value so I cannot use it for insert value. So my question is ... How to make possible code like

DECLARE @code VARCHAR(MAX)
SET @code = DBO.GENCODE()

    INSERT INTO TABLEA(CODE)
    VALUES(@code)

And this working snippet as a version...

DECLARE @code VARCHAR(MAX)
SET @code=(SELECT NEWID() AS GUID)
INSERT INTO TABLEA(CODE)
VALUES(@code)

?

Any useful comments are appreciated

Upvotes: 0

Views: 573

Answers (3)

user592704
user592704

Reputation: 3704

As for now I am satisfied with this code because it works fine and gens a random code

DECLARE @code VARCHAR(MAX)
SET @code=(SELECT NEWID() AS GUID)
INSERT INTO TABLEA(CODE)
VALUES(@code)

But still I hope to get it working as a function. Maybe a little bit later then :)

Upvotes: 0

Jon Seigel
Jon Seigel

Reputation: 12401

INSERT INTO TABLEA(CODE)
    EXEC DBO.GENCODE

I'm answering the question literally because I would assume there is more complicated logic inside the stored procedure than this.

Edit re the question edit: You either have to use the INSERT/EXEC trick that I show above, or use a scalar UDF. It's not possible to do that using a stored procedure.

Upvotes: 1

Bueller
Bueller

Reputation: 2344

http://msdn.microsoft.com/en-us/library/ms190348.aspx

INSERT INTO TABLEA(CODE)
VALUES (NEWID())

Upvotes: 2

Related Questions