Is it possible to set a varible based conditional values?
How I would explain it in layman's terms would be: I want my variable @L0 set up where if a CompanyCode = A then L0 = 1, If CompanyCode = B, then L0 = 2. Later in my Sproc I want to use that logic to assign a Score based on the conditions in variable L0. So it's like I want the variable L0 to be a whole condition statement where then each line will be assigned it's score based on looking at the CompanyCode.
Example of how I would think to do it, however I don't want to hard code any of the company code values.
declare @L0 int
declare @L1 int =-1
declare @StartDate datetime
declare @EndDate datetime
set @L0 = (Select TRF from [Database1].[DQ].[RFRuleConfig] R
JOIN [Database2].[Dim].[Company] C on C.[CompanyCode] = R.[CompanyCode]
WHERE C.CompanyCode IN ('1857','1848'))
set @StartDate = dateadd(month, datediff(month, 0, getdate()) - 1, 0)
set @EndDate = dateadd(month, datediff(month, 0, getdate()) + 0, -1)
Data sample from [Database1].[DQ].[RFRuleConfig]. I want @L0 variable to be set to whatever is in TRF.
| CompanyCode | TRF |
|---|---|
| 1857 | 8 |
| 1848 | 16 |
| 1617 | 7 |
Alternatively, some sort of if statement might work where I do hard code values but can still use multiple. This is just not as ideal.
Ultimately, I need the @L0 value (which would be the TRF) to assign a score, based on the CompanyCode.@L0 is called many times in my Sproc and should behave dynamically so it will assign the score. If the CompanyCode is 1857 the Score (TRF) needs to be 8, if the CompanyCode is 1848 the score needs to be 16 etc.
select Recordidinv,CompanyCode,BookCountryID, 'RF1' as Error, @L1 as Score, from [Table]
You can use a table variable much like an actual table:
You can insert into it just like any other table, using
VALUESor aSELECTstatement. This table variable will only exist in the context of the executing batch, and is gone once the batch completes.