查询过滤器确实在我的 s-s-rS 报告中返回过滤值
Posted
技术标签:
【中文标题】查询过滤器确实在我的 s-s-rS 报告中返回过滤值【英文标题】:Query filter does return filtered value in my s-s-rS report 【发布时间】:2021-09-27 15:22:19 【问题描述】:我正在创建 s-s-rS 报告,但以下查询中提到的此过滤器 starttime between @StartDateTime and @EndDateTime and UID like @uIdCondition and Type in (@Type)
不返回任何过滤值。有人可以指导我做错了什么吗?
此外,我不确定如何将所有内容合二为一,以便我的所有过滤器都能正常工作。目前,拳头if else
工作正常,但问题出在以下查询中。
这是表格脚本
CREATE TABLE [dbo].[EmpLog](
[Id] [int] NOT NULL,
[LoggedTime] [datetime] NULL,
[StartTime] [datetime] NULL,
[Name] [nvarchar](128) NULL,
[Type] [nvarchar](128) NULL,
[UID] [nvarchar](64) NULL,
CONSTRAINT [PK_EmpLog] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
插入语句
INSERT INTO EmpLog (Id, LoggedTime, StartTime, Name,Type,UID) VALUES (1,'2021-02-20 01:15:44.647','2021-02-20 01:15:44.647', 'abc', 'cde','YY');
INSERT INTO EmpLog (Id, LoggedTime, StartTime, Name,Type,UID) VALUES (2,'2021-02-21 01:15:44.647','2021-02-21 01:15:44.647', 'mmm', 'nnn','gg');
INSERT INTO EmpLog (Id, LoggedTime, StartTime, Name,Type,UID) VALUES (3,'2021-02-22 01:15:44.647','2021-02-22 01:15:44.647', 'hhh', 'kkk','ll');
这个脚本如果像这样单独放置就可以工作
declare @empNameCondition varchar(50)
if @empName = '' or @empName is null or len(@empName) = 0
Begin
select ID, LoggedTime, startTime,Type, Name, uID
from [EmpLog]
order by ID, loggedTime, UId, Name,Type
End
else
Begin
set @empNameCondition = '%' + @empName + '%'
select ID, LoggedTime, startTime,Type, Name, uID
from [EmpLog]
where Name like @empNameCondition
order by ID, loggedTime, UId, Name,Type
end
如果我单独运行第二个脚本,它也会运行:
declare @UIdcondition varchar(50)
begin
if @UId = '' or @UId is null or len(@UId) = 0
set @UIdCondition = '[a-z0-9]%'
else
set @UIdCondition = @UId
select ID, LoggedTime, startTime,Type, Name, uID
from [EmpLog] where
starttime between ''2021-02-20 01:15:44.647' and '2021-02-20 01:15:44.647'
and UID like @uIdCondition
and Type in (@Type)
end
但是当我将两个脚本组合在一起时,过滤器的最后一部分不会返回任何内容。
DECLARE @empName varchar(50)
DECLARE @UId VARCHAR(50)
DECLARE @StartDateTime VARCHAR(50)
DECLARE @EndDateTime VARCHAR(50)
declare @empNameCondition varchar(50)
set @uid='YY'
set @type ='cde'
if @empName = '' or @empName is null or len(@empName) = 0
Begin
select ID, LoggedTime, startTime,Type, Name, uID
from [EmpLog]
order by ID, loggedTime, UId, Name,Type
End
else
Begin
set @empNameCondition = '%' + @empName + '%'
select ID, LoggedTime, startTime,Type, Name, uID
from [EmpLog]
where Name like @empNameCondition
order by ID, loggedTime, UId, Name,Type
end
declare @UIdcondition varchar(50)
begin
if @UId = '' or @UId is null or len(@UId) = 0
set @UIdcondition = '[a-z0-9]%'
else
set @UIdcondition = @UId
select ID, LoggedTime, startTime,Type, Name, uID
from [EmpLog] where
starttime between ''2021-02-20 01:15:44.647' and '2021-02-20 01:15:44.647'
and UID like @uIdCondition
and Type in (@Type)
end
【问题讨论】:
你想达到什么目的?您的创建表查询没有源列 不是一个真正的最小可重现示例,也充满了语法错误。请Edit您的问题以修复错误并包括变量的定义(和值):@StartDateTime
、@EndDateTime
、@uIdCondition
和 @Type
。
嗨@KetanKotak,更新了我的问题。当我在我的 s-s-rS 报告中使用此查询时。我想将查询的第一部分和第二部分结合起来,以便我可以将其用作单个查询。
嗨@AlwaysLearning,如果我在报告中运行相同的查询。我不会收到任何语法错误。另外,正如我在问题中提到的,我想将两者结合在一个查询中。我已经提供了表结构和插入语句,所以我认为这会帮助有人帮助我。
您的插入语句仍然有错误。这里没有人可以看到你的肩膀,所以如果你想解释为什么 @StartDateTime
、@EndDateTime
和 @Type
的特定值不起作用,你必须向我们展示你是如何定义它们的以及它们的值是什么商店。
【参考方案1】:
执行以下查询以获得您想要的结果
DECLARE @empName varchar(50)
DECLARE @UId VARCHAR(50)
DECLARE @StartDateTime VARCHAR(50)
DECLARE @EndDateTime VARCHAR(50)
SET @StartDateTime = '2021-02-20 01:15:44.647'
SELECT ID, LoggedTime, startTime,Type, Name, uID
FROM [EmpLog]
WHERE
Name LIKE CASE WHEN ISNULL(@empName,'') <> '' THEN '%'+@empName+'%' ELSE Name END
AND UID LIKE CASE WHEN ISNULL(@UId,'') <> '' THEN '%'+@UId+'%' ELSE UID END
-- AND starttime between @StartDateTime and @EndDateTime --this condition is not clear in question as it should be check for all or only when @starttime parameter is passed
AND StartTime BETWEEN CASE WHEN ISNULL(@StartDateTime,'') <> '' THEN @StartDateTime ELSE '1900-01-01' END AND CASE WHEN ISNULL(@EndDateTime,'') <> '' THEN @EndDateTime ELSE '2099-12-31' END
【讨论】:
嗨@KetanKotak,您的查询中似乎没有包含我的所有条件。请简单地在下面查询所有条件。谢谢! 类型中的参数可以是什么? 时可以使用相同的语法大小写以上是关于查询过滤器确实在我的 s-s-rS 报告中返回过滤值的主要内容,如果未能解决你的问题,请参考以下文章