重构查询函数中的硬编码值
Refactor querying hardcoded values in function
有人对如何更好地重构有什么建议吗?我已重命名项目名称以简化。
我只继承了这个(用于导入过程),它对我来说看起来不是很有效,尤其是函数中的硬编码值。我正在考虑将硬编码值放入 table 并引用它而不是函数,但我不确定如何根据查询中的重复列使其工作。
MyTable
MyTableId INT,
MyTableOtherColumn VARCHAR(100),
MyTableDesc1 VARCHAR(10),
MyTableDesc2 VARCHAR(10),
. . .
MyTable24 VARCHAR(10),
MyTable25 VARCHAR(10),
etc.
MyFunction
RETURNS @myFunctionTable TABLE
(
myFunctionDesc VARCHAR(256),
myFunctionCode VARCHAR(10)
)
AS
BEGIN
INSERT INTO @myFunctionTable (myFunctionDesc, myFunctionCode) VALUES ('My Function Desc 1', 'MYCODE1');
INSERT INTO @myFunctionTable (myFunctionDesc, myFunctionCode) VALUES ('My Function Desc 2', 'MYCODE2');
. . .
INSERT INTO @myFunctionTable (myFunctionDesc, myFunctionCode) VALUES ('My Function Desc 99', 'MYCODE99');
INSERT INTO @myFunctionTable (myFunctionDesc, myFunctionCode) VALUES ('My Function Desc 100', 'MYCODE100');
END
SELECT
MyTableId,
MyTableOtherColumn,
. . .
(SELECT myFunctionCode FROM MyFunction() WHERE MyFunctionDesc = MyTableDesc1),
(SELECT myFunctionCode FROM MyFunction() WHERE MyFunctionDesc = MyTableDesc2),
. . .
(SELECT myFunctionCode FROM MyFunction() WHERE MyFunctionDesc = MyTableDesc24),
(SELECT myFunctionCode FROM MyFunction() WHERE MyFunctionDesc = MyTableDesc25),
FROM myTable T
JOIN . . .
WHERE . . .
只需使用条件聚合和 outer apply
:
SELECT MyTableId, MyTableOtherColumn,
. . .
mf.val1, mv.val2, . . .
FROM myTable T JOIN
. . . OUTER APPLY
(SELECT MAX(CASE WHEN MyFunctionDesc = MyTableDesc1 THEN myFunctionCode END) as val1,
MAX(CASE WHEN MyFunctionDesc = MyTableDesc2 THEN myFunctionCode END) as val2,
. . .
FROM MyFunction()
) mf
WHERE . . .
有人对如何更好地重构有什么建议吗?我已重命名项目名称以简化。
我只继承了这个(用于导入过程),它对我来说看起来不是很有效,尤其是函数中的硬编码值。我正在考虑将硬编码值放入 table 并引用它而不是函数,但我不确定如何根据查询中的重复列使其工作。
MyTable
MyTableId INT,
MyTableOtherColumn VARCHAR(100),
MyTableDesc1 VARCHAR(10),
MyTableDesc2 VARCHAR(10),
. . .
MyTable24 VARCHAR(10),
MyTable25 VARCHAR(10),
etc.
MyFunction
RETURNS @myFunctionTable TABLE
(
myFunctionDesc VARCHAR(256),
myFunctionCode VARCHAR(10)
)
AS
BEGIN
INSERT INTO @myFunctionTable (myFunctionDesc, myFunctionCode) VALUES ('My Function Desc 1', 'MYCODE1');
INSERT INTO @myFunctionTable (myFunctionDesc, myFunctionCode) VALUES ('My Function Desc 2', 'MYCODE2');
. . .
INSERT INTO @myFunctionTable (myFunctionDesc, myFunctionCode) VALUES ('My Function Desc 99', 'MYCODE99');
INSERT INTO @myFunctionTable (myFunctionDesc, myFunctionCode) VALUES ('My Function Desc 100', 'MYCODE100');
END
SELECT
MyTableId,
MyTableOtherColumn,
. . .
(SELECT myFunctionCode FROM MyFunction() WHERE MyFunctionDesc = MyTableDesc1),
(SELECT myFunctionCode FROM MyFunction() WHERE MyFunctionDesc = MyTableDesc2),
. . .
(SELECT myFunctionCode FROM MyFunction() WHERE MyFunctionDesc = MyTableDesc24),
(SELECT myFunctionCode FROM MyFunction() WHERE MyFunctionDesc = MyTableDesc25),
FROM myTable T
JOIN . . .
WHERE . . .
只需使用条件聚合和 outer apply
:
SELECT MyTableId, MyTableOtherColumn,
. . .
mf.val1, mv.val2, . . .
FROM myTable T JOIN
. . . OUTER APPLY
(SELECT MAX(CASE WHEN MyFunctionDesc = MyTableDesc1 THEN myFunctionCode END) as val1,
MAX(CASE WHEN MyFunctionDesc = MyTableDesc2 THEN myFunctionCode END) as val2,
. . .
FROM MyFunction()
) mf
WHERE . . .