0

出于某种原因,我无法获得此存储过程的正确语法。我有 2 个变量。问题是——当我们执行存储过程时,用户可能需要为参数提供 1 个整数、多个整数或无。我想在此过程中允许用户使用这两个参数的所有选项。以下是我目前所拥有的。我一直在尝试不同的方法,看来这应该很容易解决。

现在,它似乎不喜欢 ELSE。我在“ELSE”一词附近得到了不正确的语法。当我取出这些时,我得到了所有 4 个查询的结果窗口,即使其中只有 1 个 IF 可能是真的。

任何人都可以帮忙吗?

ALTER PROCEDURE sp_ProjectDocs_AuditDB_ByLB 
@BID VARCHAR = NULL, @LID VARCHAR = NULL
AS
BEGIN
    SET NOCOUNT ON

    --DECLARE @BID VARCHAR
    --, @LID VARCHAR 
    --SET @BID = '301,316,373,322,331'
    ----'301,316,373,322,331'
    --SET @LID = '1'
    ----'1,2,3,4'

    IF ISNULL(@BID,'') = '' AND ISNULL(@LID,'') = ''
        PRINT 'BOTH NULL'
        BEGIN
            SELECT VWPD.*
            FROM vw_ProjectDocs_AuditDB VWPD
        END
    ELSE
        IF ISNULL(@BID,'') = '' AND ISNULL(@LID,'') <> ''
            PRINT 'LID NULL'
            BEGIN
                SELECT VWPD.*
                FROM vw_ProjectDocs_AuditDB VWPD
                WHERE VWPD.LID IN (@LID)
            END
    ELSE        
        IF ISNULL(@BID,'') <> '' AND ISNULL(@LID,'') = ''
            PRINT 'BID NULL'
            BEGIN
                SELECT VWPD.*
                FROM vw_ProjectDocs_AuditDB VWPD
                WHERE VWPD.BID IN (@BID)
            END
    ELSE
    --IF ISNULL(@BID,'') <> '' AND ISNULL(@LID,'') <> ''
        PRINT 'NEITHER NULL'
        BEGIN
            SELECT VWPD.*
            FROM vw_ProjectDocs_AuditDB VWPD
            WHERE VWPD.LID IN (@LID) AND VWPD.BID IN (@BID)

        END
END
GO

--EXEC sp_ProjectDocs_AuditDB_ByLB '301','1'

GO

更新了 sql,以解决问题。这似乎可以运行,但是当我可以通过将相同的参数直接传递给 select 语句中的视图来验证我这样做时,我没有得到任何结果。

ALTER PROCEDURE sp_ProjectDocs_AuditDB_ByLB 
@BID VARCHAR = NULL, @LID VARCHAR = NULL
AS
BEGIN
    SET NOCOUNT ON

    --DECLARE @BID VARCHAR(100)
    --, @LID VARCHAR 
    --SET @BID = '301,316,373,322,331'
    ----'301,316,373,322,331'
    --SET @LID = '1'
    ----'1,2,3,4'

    IF ISNULL(@BID,'') = '' AND ISNULL(@LID,'') = ''
        BEGIN
            --PRINT 'BOTH NULL'
            SELECT VWPD.*
            FROM vw_ProjectDocs_AuditDB VWPD
        END
    ELSE
        IF ISNULL(@BID,'') = '' AND ISNULL(@LID,'') <> ''
            BEGIN
                --PRINT 'BID NULL'
                SELECT VWPD.*
                FROM vw_ProjectDocs_AuditDB VWPD
                WHERE VWPD.LID IN (@LID)
            END
    ELSE        
        IF ISNULL(@BID,'') <> '' AND ISNULL(@LID,'') = ''
            BEGIN
                --PRINT 'LID NULL'
                SELECT VWPD.*
                FROM vw_ProjectDocs_AuditDB VWPD
                WHERE VWPD.BID IN (@BID)
            END
    ELSE
    --IF ISNULL(@BID,'') <> '' AND ISNULL(@LID,'') <> ''
        BEGIN
            --PRINT 'NEITHER NULL'
            SELECT VWPD.*
            FROM vw_ProjectDocs_AuditDB VWPD
            WHERE VWPD.BID IN (@BID) AND VWPD.LID IN (@LID)

        END
END
GO

--EXEC sp_ProjectDocs_AuditDB_ByLB '301','1'

GO
4

3 回答 3

3

你必须把PRINT里面的BEGIN/END块。

请注意,'LID NULL'根据'BID NULL'您的IF.

于 2016-07-06T13:31:00.107 回答
2

试试这个,你必须在块Print内给出声明。Begin-End

ALTER PROCEDURE sp_ProjectDocs_AuditDB_ByLB 
@BID VARCHAR = NULL, @LID VARCHAR = NULL
AS
BEGIN
    declare @sqlquery varchar(max)

    SET NOCOUNT ON

    --DECLARE @BID VARCHAR
    --, @LID VARCHAR 
    --SET @BID = '301,316,373,322,331'
    ----'301,316,373,322,331'
    --SET @LID = '1'
    ----'1,2,3,4'

    IF ISNULL(@BID,'') = '' AND ISNULL(@LID,'') = ''

        BEGIN
        set @sqlquery = '
        PRINT ''BOTH NULL''
            SELECT VWPD.*
            FROM vw_ProjectDocs_AuditDB VWPD'
        END
    ELSE
        IF ISNULL(@BID,'') = '' AND ISNULL(@LID,'') <> ''

            BEGIN
            set @sqlquery = '
            PRINT ''LID NULL''
                SELECT VWPD.*
                FROM vw_ProjectDocs_AuditDB VWPD
                WHERE VWPD.LID IN(' + @LID+')'
            END
    ELSE        
        IF ISNULL(@BID,'') <> '' AND ISNULL(@LID,'') = ''

            BEGIN
            set @sqlquery = '
            PRINT ''BID NULL''
                SELECT VWPD.*
                FROM vw_ProjectDocs_AuditDB VWPD
                WHERE VWPD.BID IN ('+ @BID+')'
            END
    ELSE
    --IF ISNULL(@BID,'') <> '' AND ISNULL(@LID,'') <> ''

        BEGIN
        set @sqlquery = '
        PRINT ''NEITHER NULL''
            SELECT VWPD.*
            FROM vw_ProjectDocs_AuditDB VWPD
            WHERE VWPD.LID IN ('+@LID+') AND VWPD.BID IN ('+@BID+')'

        END
    EXEC (@sqlquery)
END
GO
于 2016-07-06T13:27:00.427 回答
1

这是最终的工作存储过程。

ALTER PROCEDURE sp_ProjectDocs_AuditDB_ByLB 
@BID NVARCHAR(500) = NULL, @LID NVARCHAR(500) = NULL
AS
BEGIN
    SET NOCOUNT ON

    --DECLARE @BID NVARCHAR(500)
    --, @LID NVARCHAR(500)
    --SET @BID = '301,316,373,322,331'
    ----'301,316,373,322,331'
    --SET @LID = '1'
    ----'1,2,3,4'


    IF ISNULL(@BID,'') = '' AND ISNULL(@LID,'') = ''
        BEGIN
            --PRINT 'BOTH NULL'
            SELECT VWPD.*
            FROM vw_ProjectDocs_AuditDB VWPD
        END
    ELSE
        IF ISNULL(@BID,'') = '' AND ISNULL(@LID,'') <> ''
            BEGIN
                --PRINT 'BID NULL'
                SELECT VWPD.*
                FROM vw_ProjectDocs_AuditDB VWPD
                WHERE VWPD.LID IN (SELECT * FROM SPLIT(@LID, ','))
            END
    ELSE        
        IF ISNULL(@BID,'') <> '' AND ISNULL(@LID,'') = ''
            BEGIN
                --PRINT 'LID NULL'
                SELECT VWPD.*
                FROM vw_ProjectDocs_AuditDB VWPD
                WHERE VWPD.BID IN (SELECT * FROM SPLIT(@BID, ','))
            END
    ELSE
    --IF ISNULL(@BID,'') <> '' AND ISNULL(@LID,'') <> ''
        BEGIN
            --PRINT 'NEITHER NULL'
            SELECT VWPD.*
            FROM vw_ProjectDocs_AuditDB VWPD
            WHERE VWPD.BID IN (SELECT * FROM SPLIT(@BID, ',')) AND VWPD.LID IN (SELECT * FROM SPLIT(@LID, ','))

        END
END
GO

--EXEC sp_ProjectDocs_AuditDB_ByLB '301','1'

GO
于 2016-07-06T14:59:57.813 回答