出于某种原因,我无法获得此存储过程的语法。我有2个变量。问题是这样的 - 当我们执行存储过程时,用户可能需要为参数,多个或无提供1个整数。对于这两个参数,我想在此过程中允许用户使用所有选项。以下是我现在的情况。我一直在尝试不同的方法,看起来这应该很容易解决。
目前,它似乎并不喜欢ELSE。我在单词' ELSE'附近的语法不正确。当我拿出这些时,我得到所有4个查询的结果窗口,即使这些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
答案 0 :(得分:3)
您必须将PRINT
置于BEGIN/END
区块内。
请注意,根据您'LID NULL'
,邮件'BID NULL'
和IF
不正确。
答案 1 :(得分: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
答案 2 :(得分: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