[英]How to set Default Value of Parameter in SQL to ALL
I have 3 parameters in my table called @StartDate
, @EndDate
, and @ServerName
. 我的表中有3个参数,分别称为
@StartDate
, @EndDate
和@ServerName
。 I am wanting all 3 of these to have a default value set to ALL. 我希望所有这些3的默认值都设置为ALL。
Users have the ability to choose a StartDate
and an EndDate
and it will return all stopped services during those dates. 用户可以选择
StartDate
和EndDate
,它将在这些日期返回所有停止的服务。 I am also wanting them to be able to search for a ServerName
and it will return all records of that ServerName
no matter when the service stopped. 我还希望他们能够搜索
ServerName
,无论服务何时停止,它都将返回该ServerName
所有记录。
My problem is searching for a server name
. 我的问题是搜索
server name
。 Right now I just have a temporary fix for the parameters which would be that the default of the @StartDate
parameter is set to the beginning of the year which is a date that is before any records were logged and the @EndDate
is set to the current date. 现在,我只是临时修复了参数,这就是将
@StartDate
参数的默认值设置为年初,该日期是记录任何记录之前的日期,而@EndDate
设置为当前日期。日期。 So when the report is opened it shows all the records of stopped services and then the user can adjust the start and end dates as they'd like. 因此,当报表打开时,它会显示所有已停止服务的记录,然后用户可以根据需要调整开始和结束日期。 But when I search for a
ServerName
I want it to just find all records of that ServerName
but it still just returns ALL the records within the Start and End Dates. 但是,当我搜索
ServerName
我希望它只查找该ServerName
所有记录,但它仍然只返回Start和End Date中的所有记录。
Here is my query: 这是我的查询:
SELECT ServerName, ServiceName, Status, Date, Time
FROM ServicesStatus
WHERE (Date BETWEEN @StartDate AND @EndDate) OR (ServerName = @ServerName)
ORDER BY Date DESC, Time DESC, ServerName
So what I need is to get the @StartDate
and @EndDate
to default to all dates so when someone searches for a ServerName
it will return all instances of that server in the SQL database. 因此,我需要将
@StartDate
和@EndDate
设置为所有日期的默认值,以便当有人搜索ServerName
,它将在SQL数据库中返回该服务器的所有实例。
I also need the same for the parameter @ServerName
so when someone search for a time range, it will return with ALL the Server Names that have a stopped service during that time. 我还需要对
@ServerName
参数使用相同的参数,因此当某人搜索时间范围时,它将返回在该时间段内具有已停止服务的所有服务器名称。
You can set your default parameter values as NULL
with OR
operator on where
clause. 您可以在
where
子句中使用OR
运算符将默认参数值设置为NULL
。
If the user didn't use any condition, then it will select all datas. 如果用户未使用任何条件,则它将选择所有数据。
DECLARE @StartDate DATETIME = NULL
DECLARE @EndDate DATETIME = NULL
DECLARE @ServerName VARCHAR(100) = NULL
SELECT ServerName, ServiceName, Status, Date, Time
FROM ServicesStatus
WHERE
(Date >= @StartDate OR @StartDate IS NULL)
AND
(Date <= @EndDate OR @EndDate IS NULL)
OR
(ServerName = @ServerName OR @ServerName IS NULL)
ORDER BY Date DESC, Time DESC, ServerName
Here is a default parameter sampleFiddle 这是默认参数sampleFiddle
NOTE 注意
Your default value can be other (like 'ALL'
) that don't have to be NULL
. 您的默认值可以是其他值(例如
'ALL'
),这些值不必为NULL
。 You can default your parameter. 您可以默认参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.