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

Answers


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

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

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.


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 :)


Need Your Help

Enumerate instances of a factor level

r plyr reshape2

I have a data frame with 150000 lines in long format with multiple occurences of the same id variable. I'm using reshape (from stat, rather than package=reshape(2)) to convert this to wide format. ...

Directly calling url with Backbone

javascript backbone.js

If I navigate to a view by clicking on a link such as 127.0.0.1/#/project/1, the correct view gets displayed. However, if I call this url directly in the browser (or hit refresh), the view won't be

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.