Hello experts,
my question is simply:
how can you assign 2 values to a variable and make the sql below work?
please note that i have declared the variable at the top of the code and assigned it 1 value however i would like it to take more than one value as you can see in the comment right next to the actual declare. You will see that the actual variable @Cat is used twice in the sql.
DECLARE @Cat1 varchar(3) = 'LSS'; --('LSS','LBS')
-- Source Mapping
with stage1 as (
select distinct 'WASP' "Source System",
ac.DESCRIPT "Category Description",
dl.CATEGORY "Category Code",
case when dl.FIELD_PROMPT is not NULL
then dl.FIELD_PROMPT
else dc.DESCRIPT
end "Attribute Name",
'WASP.EMS_ASSET_DETAILS' "Table",
'DETAIL_TEXT' "Field Name",
--NULL "Sample Data",
case when dc.DATA_TYPE = 'D'
then 'Date'
else case when (dl.FIELD_STYLE = '0' or dl.FIELD_STYLE is NULL)
then 'String'
else 'Enumerator'
end
end "Data Type",
case when d.REQUIRED = '-1'
then 'Y'
else 'N'
end "Mandatory",
dl.DETAIL_CODE "Detail Code",
case when (dl.FIELD_STYLE <> '0' and dl.FIELD_STYLE is not NULL and dc.REFERENCE_TABLE is NULL)
then 'AD_LOOKUP'
else dc.REFERENCE_TABLE
end "Reference Table",
case when (dl.FIELD_STYLE <> '0' and dl.FIELD_STYLE is not NULL and dc.REFERENCE_TABLE is NULL)
then 'L_CODE'
else dc.REF_KEYCOL
end "Reference Table Column",
case when (dl.FIELD_STYLE <> '0' and dl.FIELD_STYLE is not NULL and dc.REFERENCE_TABLE is NULL)
then concat('DETAIL_CODE = ',CHAR(39),dl.DETAIL_CODE,CHAR(39))
else dc.REF_CRITERIA
end "Reference Criteria"
from [WASP].[EMS_DETAIL_LAYOUT_curr_VW] dl
left join [WASP].[EMS_ASSET_CATEGORY_curr_VW] ac on dl.CATEGORY = ac.CATEGORY
left join [WASP].[EMS_DETAIL_CODE_curr_VW] dc on dl.DETAIL_CODE = dc.DETAIL_CODE
left join [WASP].[EMS_DETAILS_curr_VW] d on dl.DETAIL_CODE = d.DETAIL_CODE and dl.CATEGORY = d.CATEGORY
where dl.CATEGORY = @Cat1 /* Load Break Switch */ --- change this depending on UoM
and dl.GROUP_NO not in ('22','40') -- omit obsolete Legacy and unused tabs in WASP
),
stage2 as (
select ad.DETAIL_CODE "Detail Code",
max(al.DESCRIPT) "Sample Data1",
max(ad2.DETAIL_TEXT) "Sample Data2"
from [WASP].[EMS_ASSET_curr_VW] a
left join [WASP].[EMS_ASSET_DETAILS_curr_VW] ad on a.ASSET_ID = ad.ASSET_ID
left join [WASP].[EMS_AD_LOOKUP_curr_VW] al on ad.DETAIL_TEXT = al.L_CODE
and ad.DETAIL_CODE = al.DETAIL_CODE and al.DESCRIPT not in ('Unknown','unkown')
left join [WASP].[EMS_ASSET_DETAILS_curr_VW] ad2 on a.ASSET_ID = ad2.ASSET_ID
and ad.DETAIL_CODE = ad2.DETAIL_CODE and ad2.DETAIL_TEXT not in ('UNKNOWN','Unknown','unkown')
where a.CATEGORY = @Cat1 -- change according to UoM
and a.SERVICE_STATUS = 'I'
group by ad.DETAIL_CODE
),
stage3 as (
select s.[Detail Code], coalesce(s.[Sample Data1], s.[Sample Data2])sample_data
from stage2 s
)
select s1.[Source System], s1.[Category Description], s1.[Category Code], s1.[Attribute Name]
, s1.[Table], s1.[Field Name], s3.sample_data, s1.[Data Type], s1.[Mandatory], s1.[Detail Code]
, s1.[Reference Table], s1.[Reference Table Column], s1.[Reference Criteria]
from stage1 s1
left join stage3 s3 on s1.[Detail Code] = s3.[Detail Code]
order by 2
Any suggestions welcome.
thank you!