组合填充两个单独变量的两个 case 表达式的方法?

Way to combine two case expressions that populate two separate variables?

我有一个存储过程,它接受用户输入,然后 returns 两个值。目前我有 2 个 case-when 表达式可以为我完成这项工作,但我想弄清楚我是否可以将这 2 个表达式组合起来以获得更简单、更整洁的代码。

下面是我当前的 case-when 代码

DECLARE @SpecialsVariable varchar(max)
DECLARE @SpecialName varchar(64) 

SET @SpecialsVariable = CASE @Special
                           WHEN 'Petr_analysis' THEN 'RAW_ColloTel,RAW_DetroCol'
                           WHEN 'AFT_analysis' THEN 'RAW_Defo,RAW_Defr,RAW_Floo,RAW_Flor'
                           WHEN 'Fisch_analysis' THEN 'RAW_COke,RAW_Gas,RAW_H2O,RAW_Tar'
                        END

SET @SpecialName =  CASE @Special
                       WHEN 'Petr_analysis' THEN 'Petrography'
                       WHEN 'AFT_analysis' THEN 'Ash Fusion Temperature'
                       WHEN 'Fisch_analysis' THEN 'Fischer-Tropsch'
                    END

我尝试了下面的代码,但我收到关于两个设置变量之间的逗号的错误

DECLARE @Special varchar(64) = 'Petr_analysis'
DECLARE @SpecialsVariable varchar(max)
DECLARE @SpecialName varchar(64) 

set @SpecialName, @SpecialsVariable =  case @Special
    when 'Petr_analysis' then 'Petrography', 'RAW_ColloTel,RAW_DetroCol'
    when 'AFT_analysis' then 'Ash Fusion Temperature', 'RAW_Defo,RAW_Defr,RAW_Floo,RAW_Flor'
    when 'Fisch_analysis' then 'Fischer-Tropsch', 'RAW_COke,RAW_Gas,RAW_H2O,RAW_Tar'
end

提前致谢

不,你不能。
case 表达式只能 return 每个条件的标量值。

但是,您可以使用 table 并使用 select 语句填充变量:

CREATE TABLE SpecialMapping -- Find a better name for this, please
(
    Special varchar(64),
    Name varchar(64), 
    Variable varchar(max) -- Do you really need max here? 
)

INSERT INTO SpecialMapping (Sepcial, Name, Variable) VALUES
('Petr_analysis', 'Petrography', 'RAW_ColloTel,RAW_DetroCol'),
('AFT_analysis', 'Ash Fusion Temperature', 'RAW_Defo,RAW_Defr,RAW_Floo,RAW_Flor'),
('Fisch_analysis', 'Fischer-Tropsch', 'RAW_COke,RAW_Gas,RAW_H2O,RAW_Tar');

在存储过程中:

SELECT @SpecialName = Name
     , @SpecialsVariable = Variable 
FROM SpecialMapping
WHERE WHERE Special = @Special;

我不相信你可以这样组合它们。

我认为你的选择是

  • 像第一个块一样独立设置变量
  • 嵌套 if 语句
  • 创建引用table

嵌套 if

示例
IF @Special = 'Petr_analysis' 
    BEGIN 
    (set your variables) 
    END 
ELSE IF @Special = 'AFT_analysis'
    BEGIN
    ...

引用示例 table 包含三列,例如 Special、SpecialsVariable、SpecialName

SELECT  @SpecialVariable = yt.SpecialVariable, 
        @SpecialName = yt.SpecialName 
FROM    yourtable yt 
WHERE   yt.Special = @Special

如果这是一次性要求,另一种选择可能是使用 table 值构造函数:

例如:

DECLARE @SpecialsVariable varchar(max)
DECLARE @SpecialName varchar(64) 
DECLARE @Special varchar(30) =  'Petr_analysis'

select @SpecialsVariable = tab.Variable, @SpecialName = tab.Name from
    (VALUES 
         ('Petr_analysis', 'RAW_ColloTel,RAW_DetroCol',  'Petrography'),
         ('AFT_analysis', 'RAW_Defo,RAW_Defr,RAW_Floo,RAW_Flor', 'Ash Fusion Temperature'),
         ('Fisch_analysis', 'RAW_COke,RAW_Gas,RAW_H2O,RAW_Tar', 'Fischer-Tropsch')         
    ) tab (Special, Variable, Name) 
    where tab.Special = @Special

关于 Table 值构造函数的信息:

https://docs.microsoft.com/en-us/sql/t-sql/queries/table-value-constructor-transact-sql?view=sql-server-ver15