MSSQL+Symfony 4.4 存储过程空结果主义

MSSQL+Symfony 4.4 Stored Procedure empty result in doctrine

我有一个存储过程。当我从 SQL Server Management Studio 执行它时,一切正常,我得到了结果。但是当我按照我的原则执行它时,我总是得到一个空结果,它是否成功并不重要。

CREATE procedure [dbo].[approve_pre_register_demand]
    @hashLink varchar(max) AS
BEGIN
    DECLARE @bClientId uniqueidentifier; 
    -- check is token valid and not deactivated
    BEGIN
        --get uuid by link
        SELECT @bClientId = [dbo].[b_client_pre_registration_link].[b_client_uuid] 
        FROM [dbo].[b_client_pre_registration_link]
        WHERE [dbo].[b_client_pre_registration_link].[hash_link] LIKE @hashLink
    END
    BEGIN
        IF NOT EXISTS(SELECT [b_client_uuid] FROM [dbo].[b_client_pre_registration_link] WHERE [hash_link] LIKE @hashLink)
            BEGIN
                RETURN 'Token does not exists'
            END
    END
    BEGIN
        IF NOT EXISTS(SELECT [dbo].[sa_temp_b_client].[uuid] FROM [dbo].[sa_temp_b_client] WHERE [dbo].[sa_temp_b_client].[uuid] LIKE @bClientId)
            BEGIN
                RETURN 'BClient does not exists'
            END
    END
    BEGIN
        IF NOT EXISTS(SELECT [dbo].[at_b_client_files].[b_client_uuid] FROM [dbo].[at_b_client_files] WHERE [dbo].[at_b_client_files].[b_client_uuid] LIKE @bClientId)
            BEGIN
                RETURN 'BClient files does not exists'
            END
    END
    -- move preregistration to permanent tables
    BEGIN
        BEGIN
            BEGIN TRANSACTION
                BEGIN

                    IF (@@error <> 0)
                        -- Отменить транзакцию, если есть ошибки
                        ROLLBACK
                END
            COMMIT
            SELECT 'RESULT' = @hashLink
        END
    END
END

在你最后的声明中,你返回:

SELECT 'RESULT' = @hashLink

如果您在 SQL Server Management Studio 或 Azure Data Studio 中对此进行测试,您将看到上面生成的一行和一列名为 RESULT:

DECLARE  @hashLink varchar(max) = 'test'
 
SELECT 'RESULT' = @hashLink;

在其他情况下你有 RETURN,这将产生一个错误:

CREATE PROCEDURE dbo.TEST 
(
    @hashLink varchar(max)
)
AS
BEGIN;

    RETURN 'text'

END;

GO

EXEC dbo.TEST @hashLink = 'x'

喜欢以下内容:

Msg 245, Level 16, State 1, Procedure dbo.TEST, Line 8 [Batch Start Line 14] Conversion failed when converting the varchar value 'text' to data type int.

Completion time: 2020-12-01T08:17:03.7036860+02:00

所以,只需将 RETURN 语句更改为 SELECTalias.


另外,如果你想用一个RETURN来获取一个值,这个值是这样被消耗的:

CREATE OR ALTER PROCEDURE dbo.TEST 
(
    @hashLink varchar(max)
)
AS
BEGIN;

    RETURN  5

END;

GO

DECLARE @A INT;

EXEC @A = dbo.TEST @hashLink = 'x'

SELECT @A;