通过选择返回值返回

时间:2018-08-06 15:59:54

标签: sql-server tsql

具有下面的mssql存储过程。我想根据结果insdie sp返回值0或1并从sp返回。在我的示例中,当我调试它并到达它时(我在查询中用第二个箭头标记)注释点不会到达SELECT 0 / RETURN 0行,但是在窗口中我可以看到“无列名” 0-意味着符合预期,但当我使用其他输入值进行调试时,当到达(第一个箭头)注释时,它也不会转到SELECT 1 / RETURN 1,并且没有像我期望的那样出现“ No column name” 1之类的消息。为什么在调试器中,它在到达SELECT/RETURN statemts之前中断了注释的存储过程,以及为什么SELECT 1 / RETURN 1没有结果。我在做什么错了?

USE [temp]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


ALTER PROCEDURE [dbo].[IsPhraseConnectedAlready]
    @PhraseId INT,
    @KatSubkatId INT,   
    @WordId INT,
    @SubsubkatId INT = NULL
AS
BEGIN
    SET NOCOUNT OFF;

    DECLARE @temp_T_Html_Word_Categories TABLE(Id INT)

    --spr. poziom 2-gi oraz wszystkie 3-cie
    IF @SubsubkatId IS NULL

        BEGIN
            INSERT INTO @temp_T_Html_Word_Categories(Id) SELECT Id FROM T_Html_Word_Categories WHERE FK_KatSubkat_ID = @KatSubkatId And FK_Word_ID = @WordId;


        Declare @id int;
        WHILE EXISTS(SELECT * FROM @temp_T_Html_Word_Categories)        
                BEGIN
                    Select Top 1 @id = Id From @temp_T_Html_Word_Categories;

                            BEGIN 
                                IF EXISTS (SELECT * FROM T_Html_WordCat_Phrase WHERE FK_Word_Categorie_ID=@id AND FK_Phrase_ID=@PhraseId)
                                    --INSERT SOMETHING
                                            --there is match existing already in db
                                        SELECT 1
                                        RETURN 1
                            END

                    Delete @temp_T_Html_Word_Categories Where Id = @Id;     
                END;

        --no match existing in db
        SELECT 0
        RETURN 0

        END


    --spr. ten konkretny poziom 3-ci oraz 2-gi
    ELSE
        BEGIN       
            INSERT INTO @temp_T_Html_Word_Categories(Id) SELECT Id FROM T_Html_Word_Categories WHERE FK_KatSubkat_ID = @KatSubkatId And FK_Word_ID = @WordId And FK_Subsubkat_ID = @SubsubkatId;
            INSERT INTO @temp_T_Html_Word_Categories(Id) SELECT Id FROM T_Html_Word_Categories WHERE FK_KatSubkat_ID = @KatSubkatId And FK_Word_ID = @WordId And FK_Subsubkat_ID IS NULL;


        Declare @id2 int;
        WHILE EXISTS(SELECT * FROM @temp_T_Html_Word_Categories)        
                BEGIN
                    Select Top 1 @id2 = Id From @temp_T_Html_Word_Categories;

                            BEGIN 
                                IF EXISTS (SELECT * FROM T_Html_WordCat_Phrase WHERE FK_Word_Categorie_ID=@id2 AND FK_Phrase_ID=@PhraseId)
                                    --INSERT SOMETHING
                                            --there is match existing already in db<--------------------------------------------------------
                                    SELECT 1
                                    RETURN 1
                            END



                    Delete @temp_T_Html_Word_Categories Where Id = @id2;        
                END

        --no match existing in db<--------------------------------------------------------
        SELECT 0
        RETURN 0

        END
END

致@Sean Lange:

请注意,当@SubsubkatId的输入值为NULL时,如在我的sp中一样,必须使用此查询:

INSERT INTO @temp_T_Html_Word_Categories(Id) SELECT Id FROM T_Html_Word_Categories WHERE FK_KatSubkat_ID = @KatSubkatId And FK_Word_ID = @WordId;

当有somethnig(不为null)时,则必须使用这些查询(也在我的sp中):

INSERT INTO @temp_T_Html_Word_Categories(Id) SELECT Id FROM T_Html_Word_Categories WHERE FK_KatSubkat_ID = @KatSubkatId And FK_Word_ID = @WordId And FK_Subsubkat_ID = @SubsubkatId;
            INSERT INTO @temp_T_Html_Word_Categories(Id) SELECT Id FROM T_Html_Word_Categories WHERE FK_KatSubkat_ID = @KatSubkatId And FK_Word_ID = @WordId And FK_Subsubkat_ID IS NULL;

我不确定您的简化版本是否与之匹配。

1 个答案:

答案 0 :(得分:1)

不太确定想要什么,但是可以确定可以将ENTIRE过程简化为这样的单个语句。绝对不需要循环并使之变得如此复杂。

ALTER PROCEDURE [dbo].[IsPhraseConnectedAlready]
    @PhraseId INT,
    @KatSubkatId INT,   
    @WordId INT,
    @SubsubkatId INT = NULL
AS
BEGIN
    SET NOCOUNT OFF;

    select DoesItExist = convert(bit, count(*)) 
    from T_Html_Word_Categories c
    join T_Html_WordCat_Phrase wcp pm wcp.FK_Word_Categorie_ID = c.ID AND FK_Phrase_ID=@PhraseId
    WHERE c.FK_KatSubkat_ID = @KatSubkatId 
        And c.FK_Word_ID = @WordId
        And c.FK_Subsubkat_ID = isnull(@SubsubkatId, c.FK_Subsubkat_ID)

END