我有一个存储过程,它会从我们的数据库中返回一个结果列表。所有选择查询都与最后一个WHERE行分开。即使@ACTIVE变量设置为true,存储的proc仍会带回GLEV_STATUS = 1的结果,它应该只返回GLEV_STATUS = 0的结果。我想我刚用错误的语法编写了但是我尝试了很多将节点放在哪里的不同组合,仍然无法将其仅仅带回GLEV_STATUS = 0的结果。
USE [DATABASE]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[sp_Outlook_Addin_Search]
@SUBJECT varchar(50),
@EVENTNO varchar(50),
@CONTACT varchar(50),
@ACTIVE varchar(50),
@OwnerUserName varchar(200),
@OwnerEmailName varchar(200),
@OwnerEmail varchar(200)
AS
BEGIN
DECLARE @User varchar(100)
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Declare a return integer
Declare @ReturnedCount as int;
-- Find the User that is searching
SET @User = (SELECT USER_SYS_NO FROM DB.f_ad_users WITH(NOLOCK)
WHERE user_full_name = @OwnerUserName OR user_full_name = @OwnerEmailName OR user_email_address = @OwnerEmail OR user_log_name = @OwnerUserName OR user_log_name = @OwnerEmailName)
-- Call the new outlook count events function to find out how many possible records there are.
set @ReturnedCount = [dbo].[fn_Outlook_CountEvents](@SUBJECT, @EVENTNO, @User, @CONTACT, @ACTIVE);
-- Select DF Events Table
SELECT DISTINCT TOP 20 EVST_EVENT_TYPE
, EVST_EDITABLE_BY
, GLEV_SYS_NO
, GLEV_USER_INIT
, cast(GLEV_SUBJECT as varchar(MAX)) as GLEV_SUBJECT
, cast(GLEV_NOTES as varchar(MAX)) as GLEV_NOTES
, EVST_ALLOW_NOTE_EDITING
, EVST_DEFAULT_TYPE
, CONT_FIRST_NAME
, CONT_SURNAME
, GLEV_STATUS
, @ReturnedCount as CountValue
FROM DeFactoUser.F_GL_Events with (nolock)
INNER JOIN DB.F_GL_Event_Status with(NOLOCK)
ON GLEV_EVENT_TYPE = EVST_EVENT_TYPE
LEFT JOIN DB.F_GL_Contacts with (NOLOCK)
ON CONT_CONCAT = GLEV_CONTACT
LEFT JOIN DB.F_GL_Event_Distribution with (NOLOCK)
ON EVDN_GLEV_SYS_NO = GLEV_SYS_NO
WHERE (GLEV_SUBJECT like ('%' + @SUBJECT + '%')
AND GLEV_SYS_NO like (@EVENTNO + '%'))
AND (CONT_FIRST_NAME + CONT_SURNAME LIKE ('%' + @CONTACT + '%') OR (@CONTACT = '' AND CONT_FIRST_NAME IS NULL))
AND ((EVST_DEFAULT_TYPE = 1) OR (EVST_DEFAULT_TYPE = 0 AND (GLEV_USER_INIT = @User OR EVDN_USER_SYS_NO = @User)) OR (EVST_DEFAULT_TYPE = 2 AND GLEV_USER_INIT = 8))
AND ((EVST_EDITABLE_BY = 0) OR (EVST_EDITABLE_BY = 1 AND GLEV_USER_INIT = @User) OR (EVST_EDITABLE_BY = 2 AND EVDN_USER_SYS_NO = @User))
AND ((@ACTIVE = 'false' AND GLEV_STATUS = 0 OR GLEV_STATUS = 1) OR (@ACTIVE = 'true' AND GLEV_STATUS = 0))
ORDER BY GLEV_SYS_NO DESC
END
这一行是问题所在:
AND ((@ACTIVE = 'false' AND GLEV_STATUS = 0 OR GLEV_STATUS = 1) OR (@ACTIVE = 'true' AND GLEV_STATUS = 0))
执行SP:
USE [DATABASE]
GO
DECLARE @return_value int
EXEC @return_value = [dbo].[sp_Outlook_Addin_Search]
@SUBJECT = N'',
@EVENTNO = N'',
@CONTACT = N'',
@ACTIVE = N'true',
@OwnerUserName = N'Name',
@OwnerEmailName = NULL,
@OwnerEmail = NULL
SELECT 'Return Value' = @return_value
GO
答案 0 :(得分:3)
您缺少一些支架
改变
AND (
(@ACTIVE = 'false' AND GLEV_STATUS = 0 OR GLEV_STATUS = 1)
OR (@ACTIVE = 'true' AND GLEV_STATUS = 0)
)
进入
AND (
(@ACTIVE = 'false' AND (GLEV_STATUS = 0 OR GLEV_STATUS = 1))
OR (@ACTIVE = 'true' AND GLEV_STATUS = 0)
)
如果active为false且status为0,则第一个将返回该行,如果glev status为1,则返回该行。