I have a database table, ‘Reports’. Each column in this table is of type bit. So if value 1 (i.e. true) then report is required.
I have a Stored Procedure which is used to populate a temp table with ALL reports marked as true.
What is the best way to do this? My CASE statement returns only the first TRUE value, when what I want is all cases where report is TRUE.
Thanks!
DECLARE @RequiredReports as table
(
Report nvarchar(150)
)
INSERT INTO @RequiredReports
(
Report
)
SELECT
CASE
WHEN r.ReportCountry = 1 THEN 'Country Report'
WHEN r.ReportPerson = 1 THEN 'Person Report'
WHEN r.ReportProfession = 1 THEN 'Profession Report'
WHEN r.ReportAge = 1 THEN 'Age Report'
END
FROM dbo.Reports r
You can use cross apply like this:
https://data.stackexchange.com/stackoverflow/query/61227/unpivot-reports
In SQL Server 2008 and later you can use
valuesinstead ofunion all.