简体   繁体   中英

SQL Server: Returning UniqueIdentifier, Operand type clash: uniqueidentifier is incompatible with int

I'm getting this error when trying to return a unique identifier from a stored procedure - thoughts?

Operand type clash: uniqueidentifier is incompatible with int

Code:

CREATE PROCEDURE TEST
AS
    RETURN NEWID()

Stored procedures can only 'return' an INT (it's meant to be used as a status code). If you want to get other data out of a stored procedure, you should SELECT it. Accessing that data from the code making the call may be a little different, but that's how you have to do it.

For example:

CREATE PROCEDURE Test
AS
    SELECT NEWID()

Procedures in SQL Server do not return values . . . well, there is an implicit integer status, but that is not what you want.

Perhaps you intend:

CREATE PROCEDURE TEST (
    @out_id uniqueidentifier output
)
AS
BEGIN
    SET @out_id = NEWID();
END;

Or, perhaps you want to define a function:

CREATE FUNCTION TEST
RETURNS uniqueidentifier
BEGIN
    RETURN NEWID();
END;

However, I don't believe you can really do this, because newid() is considered to have side effects -- and that is not allowed in a SQL Server function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM