SQL Server:从存储过程返回uniqueidentifier

时间:2021-02-23 09:37:42

Can I return UNIQUEIDENTIFIER from a stored procedure using the RETURN statement or is it only by using the OUTPUT statement?

我可以使用RETURN语句从存储过程返回UNIQUEIDENTIFIER,还是仅使用OUTPUT语句?

i.e to return the PersonID UNIQUEIDENTIFIER:

即返回PersonID UNIQUEIDENTIFIER:

CREATE PROCEDURE CreatePerson
    @Name NVARCHAR(255), 
    @Desc TEXT
AS
DECLARE @Count INT
DECLARE @JobFileGUID UNIQUEIDENTIFIER 

-- Check if job exists?
SET @Count = (SELECT COUNT(Name) AS Name FROM Person WHERE Name=@Name)

IF @Count < 1
BEGIN
    SET @PersonGUID = NEWID();

    INSERT INTO Person 
        (PersonID, Name, [Desc]) 
        VALUES (@PersonGUID, @Name, @Desc)

END

    SELECT @PersonGUID = Person.PersonID 
    FROM Person
    WHERE Name = @Name

    RETURN @PersonGUID
GO

Thanks

谢谢

3 个解决方案

#1


6  

In stored procedure - only using the OUTPUT statement. In function - return.

在存储过程中 - 仅使用OUTPUT语句。在功能 - 返回。

#2


6  

Use:

使用:

CREATE PROCEDURE CreatePerson
    @Name NVARCHAR(255), 
    @Desc TEXT,
    @PersonGUID UNIQUEIDENTIFIER OUTPUT
AS
BEGIN

   SET @PersonGUID = ...

END

How to call:

怎么称呼:

DECLARE 
    @name NVARCHAR(255),
    @desc TEXT,
    @personGUID UNIQUEIDENTIFIER

SET @name = 'Bob'
SET @desc = 'One handsome man.'

EXEC [Database].[schema].CreatePerson @name, @desc, @personGUID OUTPUT

#3


-2  

CREATE TABLE [dbo].[tbl_Clients]( [ClientID] [uniqueidentifier] NULL, [ClientName] varchar NULL, [ClientEnabled] [bit] NULL ) ON [PRIMARY]

GO

CREATE PROCEDURE [dbo].[sp_ClientCreate] @in_ClientName varchar(250) = "New Client 123", @in_ClientEnabled bit, @out_ClientId uniqueidentifier OUTPUT AS

SET @out_ClientId = NEWID();

INSERT INTO tbl_Clients(ClientId, ClientName, ClientEnabled) VALUES( @out_ClientId, @in_ClientName, @in_ClientEnabled)

DECLARE @return_value int, @out_ClientId uniqueidentifier

EXEC @return_value = [dbo].[sp_ClientCreate] @in_ClientName = N'111', @in_ClientEnabled = 1, @out_ClientId = @out_ClientId OUTPUT

SELECT @out_ClientId as N'@out_ClientId'

SELECT 'Return Value' = @return_value

GO

Result:-59A6D7FE-8C9A-4ED3-8FC6-31A989CCC8DB

结果:-59A6D7FE-8C9A-4ED3-8FC6-31A989CCC8DB

#1


6  

In stored procedure - only using the OUTPUT statement. In function - return.

在存储过程中 - 仅使用OUTPUT语句。在功能 - 返回。

#2


6  

Use:

使用:

CREATE PROCEDURE CreatePerson
    @Name NVARCHAR(255), 
    @Desc TEXT,
    @PersonGUID UNIQUEIDENTIFIER OUTPUT
AS
BEGIN

   SET @PersonGUID = ...

END

How to call:

怎么称呼:

DECLARE 
    @name NVARCHAR(255),
    @desc TEXT,
    @personGUID UNIQUEIDENTIFIER

SET @name = 'Bob'
SET @desc = 'One handsome man.'

EXEC [Database].[schema].CreatePerson @name, @desc, @personGUID OUTPUT

#3


-2  

CREATE TABLE [dbo].[tbl_Clients]( [ClientID] [uniqueidentifier] NULL, [ClientName] varchar NULL, [ClientEnabled] [bit] NULL ) ON [PRIMARY]

GO

CREATE PROCEDURE [dbo].[sp_ClientCreate] @in_ClientName varchar(250) = "New Client 123", @in_ClientEnabled bit, @out_ClientId uniqueidentifier OUTPUT AS

SET @out_ClientId = NEWID();

INSERT INTO tbl_Clients(ClientId, ClientName, ClientEnabled) VALUES( @out_ClientId, @in_ClientName, @in_ClientEnabled)

DECLARE @return_value int, @out_ClientId uniqueidentifier

EXEC @return_value = [dbo].[sp_ClientCreate] @in_ClientName = N'111', @in_ClientEnabled = 1, @out_ClientId = @out_ClientId OUTPUT

SELECT @out_ClientId as N'@out_ClientId'

SELECT 'Return Value' = @return_value

GO

Result:-59A6D7FE-8C9A-4ED3-8FC6-31A989CCC8DB

结果:-59A6D7FE-8C9A-4ED3-8FC6-31A989CCC8DB