So now you have me seriously thinking about revamping all of my code on this project!
The two issues I have is that I have never used dynamic sql before, and not sure how it will work for me?
You see I have several queries (datasets in SSRS) that require the AND/OR conditions, due to the fact that I have multiple parameters on the report, and most are not required. This means when the user leaves the parameter NULL, I need to account for this when calling the data from the database. So as you can imagine this would call for multiple AND/OR conditions.
Below is a small snippet of what I have:
SELECT a.Year, b.franchiseID, b.RAAG_Year, b.SRR_Total, b.WO_Total, b.Estimated_Total, b.Estimated_Dol, b.Booked_Total, b.Booked_Dol, b.Completed_Total,
b.Completed_Dol, b.Sales_Dol, b.CO_DOL_ADJ
FROM (SELECT DISTINCT Year
FROM DateDim) AS a LEFT OUTER JOIN
(SELECT COALESCE (b_1.SRR_Total, 0) AS SRR_Total, COALESCE (a_1.WO_Total, 0) AS WO_Total, COALESCE (c.Estimated_Total, 0) AS Estimated_Total,
COALESCE (c.Estimated_Dol, 0) AS Estimated_Dol, COALESCE (d.Booked_Total, 0) AS Booked_Total, COALESCE (d.Booked_Dol, 0) AS Booked_Dol,
COALESCE (e.Completed_Total, 0) AS Completed_Total, COALESCE (e.Completed_Dol, 0) AS Completed_Dol, COALESCE (e.Sales_Dol, 0) AS Sales_Dol,
COALESCE (b_1.franchiseID, a_1.franchiseID, c.franchiseID, d.franchiseID) AS franchiseID, COALESCE (b_1.RAAG_Year, a_1.RAAG_Year, c.RAAG_Year,
d.RAAG_Year) AS RAAG_Year, COALESCE (f.CO_DOL_ADJ, 0) AS CO_DOL_ADJ
FROM (SELECT COUNT(workorderID) AS WO_Total, YEAR(dateCreated) AS RAAG_Year, franchiseID
FROM WRK_ORDER
WHERE (YEAR(dateCreated) = @RAAG_Year - 1) AND (franchiseID = @franchise) AND (turfID = @Turf) AND (MONTH(dateCreated) = @Month) OR
(YEAR(dateCreated) = @RAAG_Year - 1) AND (franchiseID = @franchise) AND (turfID = @Turf) AND (@Month IS NULL) OR
(YEAR(dateCreated) = @RAAG_Year - 1) AND (franchiseID = @franchise) AND (MONTH(dateCreated) = @Month) AND (@Turf IS NULL) OR
(YEAR(dateCreated) = @RAAG_Year - 1) AND (franchiseID = @franchise) AND (@Month IS NULL) AND (@Turf IS NULL) OR
(YEAR(dateCreated) = @RAAG_Year - 1) AND (turfID = @Turf) AND (MONTH(dateCreated) = @Month) AND (@franchise IS NULL) OR
(YEAR(dateCreated) = @RAAG_Year - 1) AND (turfID = @Turf) AND (@Month IS NULL) AND (@franchise IS NULL) OR
(YEAR(dateCreated) = @RAAG_Year - 1) AND (MONTH(dateCreated) = @Month) AND (@Turf IS NULL) AND (@franchise IS NULL) OR
(YEAR(dateCreated) = @RAAG_Year - 1) AND (@Month IS NULL) AND (@Turf IS NULL) AND (@franchise IS NULL)
GROUP BY YEAR(dateCreated), franchiseID) AS a_1 FULL OUTER JOIN
(SELECT COUNT(srvcReqRefID) AS SRR_Total, YEAR(dateCreated) AS RAAG_Year, franchiseID
FROM SVC_REQUEST
WHERE (YEAR(dateCreated) = @RAAG_Year - 1) AND (franchiseID = @franchise) AND (turfID = @Turf) AND (MONTH(dateCreated) = @Month) OR
(YEAR(dateCreated) = @RAAG_Year - 1) AND (franchiseID = @franchise) AND (turfID = @Turf) AND (@Month IS NULL) OR
(YEAR(dateCreated) = @RAAG_Year - 1) AND (franchiseID = @franchise) AND (MONTH(dateCreated) = @Month) AND (@Turf IS NULL) OR
(YEAR(dateCreated) = @RAAG_Year - 1) AND (franchiseID = @franchise) AND (@Month IS NULL) AND (@Turf IS NULL) OR
(YEAR(dateCreated) = @RAAG_Year - 1) AND (turfID = @Turf) AND (MONTH(dateCreated) = @Month) AND (@franchise IS NULL) OR
(YEAR(dateCreated) = @RAAG_Year - 1) AND (turfID = @Turf) AND (@Month IS NULL) AND (@franchise IS NULL) OR
(YEAR(dateCreated) = @RAAG_Year - 1) AND (MONTH(dateCreated) = @Month) AND (@Turf IS NULL) AND (@franchise IS NULL) OR
(YEAR(dateCreated) = @RAAG_Year - 1) AND (@Month IS NULL) AND (@Turf IS NULL) AND (@franchise IS NULL)
GROUP BY YEAR(dateCreated), franchiseID) AS b_1 ON a_1.franchiseID = b_1.franchiseID AND a_1.RAAG_Year = b_1.RAAG_Year
I know there is always a better way to handle a problem, so I am open to suggestions.
But, again if I can call specific Select statements based on the value of a parameter selection, then I believe that I could reduce alot of the overhead code...is this right?