我正在努力根据这些条件管理我的查询
注意:在我的存储过程中,0
表示全部,所有站点= 0,所有状态= 0
存储过程:
ALTER PROCEDURE [dbo].[SelectFineIssuanceAll_Sp]
@StatusID smallint,
@StationID smallint,
@FromDate smalldatetime,
@ToDate smalldatetime
AS
BEGIN
Begin Try
If(@StatusID = 0) --if all statuses
Begin
If(@StationID = 0) --if all stations and statuses
Begin
Select
dbo.Web_Users.Name as UserName, Issuance.Vehicles.VehicleID,
dbo.VehicleTypes.VehicleType, Issuance.LTOperator.LTName,
dbo.Stations.StationName, dbo.Statuses.Status
from
Issuance.FineIssuance
Inner Join
dbo.Web_Users ON Issuance.FineIssuance.User_ID = dbo.Web_Users.UserID
Inner Join
dbo.Statuses ON dbo.Statuses.StatusID = Issuance.FineIssuance.Status_ID
Inner Join
Issuance.Vehicles ON Issuance.Vehicles.VehicleID = Issuance.FineIssuance.Vehicle_ID
Inner Join
VehicleTypes ON dbo.VehicleTypes.VehicleTypeID = Issuance.FineIssuance.VehicleType_ID
Inner Join
Issuance.LTOperator ON Issuance.LTOperator.LTID = Issuance.FineIssuance.LTOperator_ID
Inner Join
dbo.Stations ON dbo.Stations.StationID = Issuance.FineIssuance.Station_ID
Where
Issuance.FineIssuance.IssuanceDateTime >= CONVERT(smalldatetime, @FromDate)
AND Issuance.FineIssuance.IssuanceDateTime <= CONVERT(smalldatetime, @ToDate)
End -- if all stations and statuses
Else
Begin -- else all statuses and specific station
Select dbo.Web_Users.Name as UserName, Issuance.Vehicles.VehicleID,
dbo.VehicleTypes.VehicleType, Issuance.LTOperator.LTName,
dbo.Stations.StationName, dbo.Statuses.Status from Issuance.FineIssuance
Inner Join dbo.Web_Users
ON Issuance.FineIssuance.User_ID = dbo.Web_Users.UserID
Inner Join dbo.Statuses
ON dbo.Statuses.StatusID = Issuance.FineIssuance.Status_ID
Inner Join Issuance.Vehicles
ON Issuance.Vehicles.VehicleID = Issuance.FineIssuance.Vehicle_ID
Inner Join VehicleTypes
ON dbo.VehicleTypes.VehicleTypeID = Issuance.FineIssuance.VehicleType_ID
Inner Join Issuance.LTOperator
ON Issuance.LTOperator.LTID = Issuance.FineIssuance.LTOperator_ID
Inner Join dbo.Stations
ON dbo.Stations.StationID = Issuance.FineIssuance.Station_ID
Where dbo.Stations.StationID = @StationID
AND Issuance.FineIssuance.IssuanceDateTime >= CONVERT(smalldatetime, @FromDate)
AND Issuance.FineIssuance.IssuanceDateTime <= CONVERT(smalldatetime, @ToDate)
End --else all statuses and specific station
End -- if all statuses
Else
Begin --specific station and status
Select dbo.Web_Users.Name as UserName, Issuance.Vehicles.VehicleID,
dbo.VehicleTypes.VehicleType, Issuance.LTOperator.LTName,
dbo.Stations.StationName, dbo.Statuses.Status from Issuance.FineIssuance
Inner Join dbo.Web_Users
ON Issuance.FineIssuance.User_ID = dbo.Web_Users.UserID
Inner Join dbo.Statuses
ON dbo.Statuses.StatusID = Issuance.FineIssuance.Status_ID
Inner Join Issuance.Vehicles
ON Issuance.Vehicles.VehicleID = Issuance.FineIssuance.Vehicle_ID
Inner Join VehicleTypes
ON dbo.VehicleTypes.VehicleTypeID = Issuance.FineIssuance.VehicleType_ID
Inner Join Issuance.LTOperator
ON Issuance.LTOperator.LTID = Issuance.FineIssuance.LTOperator_ID
Inner Join dbo.Stations
ON dbo.Stations.StationID = Issuance.FineIssuance.Station_ID
Where dbo.Stations.StationID = @StationID
AND dbo.Statuses.StatusID = @StatusID
AND Issuance.FineIssuance.IssuanceDateTime >= CONVERT(smalldatetime, @FromDate)
AND Issuance.FineIssuance.IssuanceDateTime <= CONVERT(smalldatetime, @ToDate)
End
End Try
Begin Catch
Select ERROR_MESSAGE() as ErrMsg
End Catch
END
请帮助我管理它,我已经解决了很多条件,但很难解决更多问题,我无法在其他条件下进一步管理,请帮助
答案 0 :(得分:2)
您可以使用此类IF / ELSE
条件实现相同的操作,而不是使用多个WHERE
块:
WHERE (@StationID = 0 OR dbo.Stations.StationID = @StationID)
AND (@StatusID = 0 OR dbo.Statuses.StatusID = @StatusID)
AND Issuance.FineIssuance.IssuanceDateTime >= CONVERT(smalldatetime, @FromDate)
AND Issuance.FineIssuance.IssuanceDateTime <= CONVERT(smalldatetime, @ToDate)
答案 1 :(得分:0)
所以不是整个查询。 (删除所有if和else) 只需在TRY Block
中写下面的查询Select dbo.Web_Users.Name as UserName, Issuance.Vehicles.VehicleID,
dbo.VehicleTypes.VehicleType, Issuance.LTOperator.LTName,
dbo.Stations.StationName, dbo.Statuses.Status from Issuance.FineIssuance
Inner Join dbo.Web_Users
ON Issuance.FineIssuance.User_ID = dbo.Web_Users.UserID
Inner Join dbo.Statuses
ON dbo.Statuses.StatusID = Issuance.FineIssuance.Status_ID
Inner Join Issuance.Vehicles
ON Issuance.Vehicles.VehicleID = Issuance.FineIssuance.Vehicle_ID
Inner Join VehicleTypes
ON dbo.VehicleTypes.VehicleTypeID = Issuance.FineIssuance.VehicleType_ID
Inner Join Issuance.LTOperator
ON Issuance.LTOperator.LTID = Issuance.FineIssuance.LTOperator_ID
Inner Join dbo.Stations
ON dbo.Stations.StationID = Issuance.FineIssuance.Station_ID
Where
(@StationID = 0 OR dbo.Stations.StationID = @StationID)
AND (@StatusID = 0 OR dbo.Statuses.StatusID = @StatusID)
AND Issuance.FineIssuance.IssuanceDateTime >= CONVERT(smalldatetime, @FromDate)
AND Issuance.FineIssuance.IssuanceDateTime <= CONVERT(smalldatetime, @ToDate)