CREATE VIEW View1
AS
SELECT Col1,
Col2,
dbo.fn1(col2) as Col3
FROM TestTable
/*
--Instead of writing below query I created a new View View2
SELECT Col1,
Col2,
dbo.fn1(col2) as Col3
dbo.fn2(dbo.fn1(col2)) as Col4
FROM TestTable
*/
CREATE VIEW View2
AS
SELECT Col1,
Col2,
Col3,
dbo.fn3(col3) as Col4
FROM TestTable
我有一个像上面这样的查询。我的列是fn1的输出。我想将该输出用于其他函数fn2。在那种情况下我不能直接在fn2中使用col3,所以我拆分了两个函数并查看如下。我想知道我是朝着正确的方向前进,我正在做什么或者有更好的方法吗?在View1的注释部分,函数fn1会调用两次吗?或者SQL Server会处理优化吗?
答案 0 :(得分:0)
你可以这样做......
SELECT
Col1,
Col2,
col3,
dbo.fn2(col3) AS Col4
FROM
(
SELECT
Col1,
Col2,
dbo.fn1(col2) as Col3
FROM
TestTable
) foo