我有一个包含单个选择查询的存储过程。查询中有几个代表公式或表达式的别名。
是否有可能提取其中的计算/公式/表达式?
在下面的例子中,我希望提取类似以下内容的内容:
“s * 2” 表示 ScoreTimesTwo
“s * 5” 表示 ScoreTimesFive
ConditionalScore 的“CASE...”(整个 case 语句)
另外,我以这样的方式编写了查询来涵盖这样一个事实:字段/表达式可以以不同的方式(等于或“AS”)分配给别名,并且表达式可以位于同一行或多行上。
我试图使用 sys.dm_exec_describe_first_result_set,但它似乎只能识别直接引用的源列(即,当它们不是公式的一部分时),而不能识别公式本身,而这正是我需要的。
/* Drop and create the test score table */
DROP TABLE IF EXISTS Test_ScoreTable
GO
CREATE TABLE Test_ScoreTable(s INT)
GO
/* Insert sample data into the score table */
INSERT INTO Test_ScoreTable(s)
SELECT t.s
FROM (VALUES (100), (50), (10)) t(s)
GO
/* Drop and create test score stored procedure */
DROP PROCEDURE IF EXISTS Test_ScoreStoredProcedure
GO
CREATE PROCEDURE Test_ScoreStoredProcedure
AS
SELECT Score = t.s,
ScoreTimesTwo = t.s * 2,
t.s * 5 AS ScoreTimesFive,
ConditionalScore = CASE t.s
WHEN 100 THEN 'Good'
WHEN 10 THEN 'Bad'
ELSE 'Eh...'
END
FROM Test_ScoreTable t
GO
/* Attempt to extract the calculations and expressions */
SELECT s.[name], s.source_column, s.is_computed_column, *
FROM sys.dm_exec_describe_first_result_set('Test_ScoreStoredProcedure', NULL, 1) s