尝试在SQL 2008R2中创建存储过程。 我有两个参数输入,根据它们的输入,理想情况下将另一个参数的值设置为四个值中的一个。试图确定是否可以这样做,如果可以,那么将它放在SP中的最佳位置在哪里。 这是我的代码的精简版。
JSON
希望用@Wear替换所有w.WearRate,并根据两个参数的组合将@Wear设置为视图中四个可能字段之一。
类似于:
{
"ids": [
84280,
2334,
235,
32554,
3663,
346,
344643,
....,
n
]
}
由于我有150多行处理此速率的代码,我宁愿不必编写此代码4次以获得所需的值。任何帮助我如何写这个并保持干净/简单将不胜感激。
答案 0 :(得分:1)
您可以将所有代码缩减为此类内容。
SELECT w.EMCo, w.Component,w.CompType, CASE
WHEN @FW = 'Y' AND @NEW = 'Y' THEN w.FwIdleWearRate
WHEN @FW = 'Y' AND @NEW = 'N' THEN w.FwWearRate
WHEN @FW = 'N' AND @NEW = 'Y' THEN w.IdleWearRate
ELSE w.StdWearRate
END AS WearRate
FROM bcvEMCompWearAdj w
答案 1 :(得分:0)
我的问题的解决方案最终是创建一个临时表并插入我想要使用的WearRate。
If OBJECT_ID('tempdb.dbo.#TempWear','U') IS NOT NULL
Drop Table #TempWear;
Create Table #TempWear
EMCo tinyint Null,
Component varchar(10) NULL,
CompType varchar(10) NULL,
WearRate decimal(38,5) NULL
);
if @FW = 'Y' and @New = 'Y'
Begin
Insert into #TempWear
(EMCo, Component, CompType, WearRate)
select w.EMCo, w.Component,w.CompType, w.FwIdleWearRate
from bcvEMCompWearAdj w
END
else
if @FW = 'Y' and @New = 'N'
Begin
Insert into #TempWear
(EMCo, Component, CompType, WearRate)
select w.EMCo, w.Component,w.CompType, w.FwWearRate
from bcvEMCompWearAdj w
END
else
if @FW = 'N' and @New = 'Y'
Begin
Insert into #TempWear
(EMCo, Component, CompType, WearRate)
sselect w.EMCo, w.Component,w.CompType, w.IdleWearRate
from bcvEMCompWearAdj w
END
else
--if @FW = 'N' and @New = 'N'
Begin
Insert into #TempWear
(EMCo, Component, CompType, WearRate)
select w.EMCo, w.Component,w.CompType, w.StdWearRate
from bcvEMCompWearAdj w
END