I’m a sql newbie, I use mssql2005
I like to do join Action depnding on input parameter.
CREATE PROCEDURE SelectPeriodicLargeCategoryData
@CATEGORY_LEVEL CHAR(1),
@CATEGORY_CODE VARCHAR(9)
AS
...
JOIN CATEGORY_AD_SYS CAS WITH(NOLOCK)
ON CA.CATEGORY_ID = [[[[[ HERE ]]]]
above the sql.
if @CATEGORY_LEVEL = 'L' then I like to join on CAS.LCATEGORY
else if @CATEGORY_LEVEL = 'M' then I like to join on CAS.MCATEGORY
else if @CATEGORY_LEVEL = 'S' then I like to join on CAS.SCATEGORY
…
how can I do this?
You could use a
CASEexpression such as:I’m not sure how fast that would be in a
JOIN‘sONcondition (depends on how smart the query optimizer is about it, of course, so you’d better check by measuring with real data) — if it turns out to have unacceptable performance, I guess you could use completely differentSELECTstatements depending on@CATEGORY_LEVELas a last resort.