我有一个理想情况下可以存在于视图中的脚本,但是因为它创建了一个临时表,SQL Server不允许使用它。
我需要能够调用它并通过各种存储过程进行联接,因此,如果需要进行更改而不是将其嵌入整个位置,最好将它包含在一个位置中。我认为存储过程不起作用,因为我不相信您可以加入SP结果集。
什么可行?
这是脚本:
<ul>
<li>one</li>
<li>two</li>
<li>three</li>
</ul>
<ul>
<li>four</li>
<li>five</li>
<li>six</li>
</ul>
<ul>
<li>seven</li>
<li>eight</li>
<li>nine</li>
</ul>
<ul>
<li>ten</li>
<li>eleven</li>
<li>twelve</li>
</ul>
答案 0 :(得分:1)
这可以作为CTE在视图内部完成。下面是代码示例,其中的表变量替换了dbo。定价显示了SQL和CTE的输出。
DECLARE @Pricing TABLE (
[FiscalPeriod] smalldatetime,
[SharePrice] DECIMAL(24,13),
[Rate] DECIMAL(24,13)
)
INSERT INTO @Pricing ([FiscalPeriod],[SharePrice],[Rate]) SELECT '2018-01-01',10.25,0.01
INSERT INTO @Pricing ([FiscalPeriod],[SharePrice],[Rate]) SELECT '2018-04-01',10.50,0.01
INSERT INTO @Pricing ([FiscalPeriod],[SharePrice],[Rate]) SELECT '2018-07-01',10.86,0.01
DECLARE @fiscalPeriod smalldatetime,
@openingUnits float,
@divPrice money,
@divFactor float,
@drip float,
@endingUnits float
DECLARE divCursor CURSOR FOR
SELECT [FiscalPeriod]
,[SharePrice]
,[Rate]
FROM @Pricing
ORDER BY FiscalPeriod
OPEN divCursor
FETCH NEXT FROM divCursor
INTO @fiscalPeriod, @divPrice, @divFactor
SET @openingUnits = 1
SET @drip = @openingUnits/@divPrice*@divFactor
SET @endingUnits = @openingUnits + @drip
SELECT
FiscalPeriod = @fiscalPeriod, OpeningUnits = @openingUnits, DivPrice = @divPrice
, DivFactor = @divFactor, DRIP = @drip, EndingUnits = @openingUnits + @drip
INTO #Temp
FETCH NEXT FROM divCursor
INTO @fiscalPeriod, @divPrice, @divFactor
WHILE @@FETCH_STATUS = 0
BEGIN
SET @openingUnits = @endingUnits
SET @drip = @openingUnits/@divPrice*@divFactor
SET @endingUnits = @openingUnits + @drip
INSERT INTO #Temp (FiscalPeriod, OpeningUnits, DivPrice, DivFactor, DRIP, EndingUnits)
VALUES (@fiscalPeriod, @openingUnits, @divPrice, @divFactor, @drip, @endingUnits)
FETCH NEXT FROM divCursor
INTO @fiscalPeriod, @divPrice, @divFactor
END
CLOSE divCursor
DEALLOCATE divCursor
SELECT * FROM #Temp
DROP TABLE #Temp;
--CTE VERSION
WITH fp_cte ([FiscalPeriod],[DivPrice],[DivFactor],[Row])
AS (
SELECT
[FiscalPeriod]
, [SharePrice] AS [DivPrice]
, [Rate] AS [DivFactor]
, ROW_NUMBER() OVER (ORDER BY [FiscalPeriod] ASC) AS [Row]
FROM @Pricing
)
,calc_cte ([FiscalPeriod],[OpeningUnits],[DivPrice],[DivFactor],[DRIP],[EndingUnits],[Row])
AS
(
SELECT
[FiscalPeriod]
, CONVERT(DECIMAL(24,13),1) AS [OpeningUnits]
, [DivPrice]
, [DivFactor]
, CONVERT(DECIMAL(24,13),1.0/[DivPrice]*[DivFactor]) AS [DRIP]
, CONVERT(DECIMAL(24,13),1+(1.0/[DivPrice]*[DivFactor])) AS [EndingUnits]
, [Row]
FROM fp_cte
WHERE [Row]=1
UNION ALL
SELECT
p2.[FiscalPeriod]
, CONVERT(DECIMAL(24,13),p1.[EndingUnits]) AS [OpeningUnits]
, p2.[DivPrice]
, p2.[DivFactor]
, CONVERT(DECIMAL(24,13),p1.[EndingUnits]/p2.[DivPrice]*p2.[DivFactor]) AS [DRIP]
, CONVERT(DECIMAL(24,13),p1.[EndingUnits]+(p1.[EndingUnits]/p2.[DivPrice]*p2.[DivFactor])) AS [EndingUnits]
, p2.[Row]
FROM calc_cte p1
INNER JOIN fp_cte p2 ON p1.[Row]=(p2.[Row]-1)
)
SELECT [FiscalPeriod],[OpeningUnits],[DivPrice],[DivFactor],[DRIP],[EndingUnits]
FROM calc_cte;
答案 1 :(得分:0)
您不能加入存储过程,但是可以通过外部应用“加入”标量函数。 https://www.sqlshack.com/the-difference-between-cross-apply-and-outer-apply-in-sql-server/
您不能在用户定义的函数中创建临时表,但可以使用表变量。 https://docs.microsoft.com/en-us/sql/relational-databases/user-defined-functions/create-user-defined-functions-database-engine?view=sql-server-2017
除此之外,我认为您可以在没有任何这些的情况下解决您的情况。待会再回来