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)
开发者_C百科
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
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 :)
精彩评论