问题描述
|
sql SERVER避免我们在sql函数中将参数传递为输出“按引用传递”
它只是允许我们在程序中这样做
有什么办法可以做到这一点?
如果不是,请有人能帮助我在Select语句中分配值
像这个例子:
Declare @X as float
set @X
select column1,column2,@X = @X+ column1+column2 FROM someView
解决方法
要产生这样的结果,您可以尝试以下解决方案:
SELECT s1.column1,s1.column2,sum(s1.column1 + s1.columns2) as res
FROM someView s1 cross join someView s2
WHERE s1.[OrderColum] <= s1.[OrderColum]
GROUP BY s1.column1,s1.columns2
但是如果您没有一些订购栏,则可以尝试此解决方案
WITH someViewWithOrder AS
(
SELECT column1,column2,ROW_NUMBER() OVER(ORDER BY (SELECT 1)) AS row_id
FROM someView
)
SELECT s1.column1,sum(s1.column1 + s1.columns2) as res
FROM someViewWithOrder s1
INNER JOIN someViewWithOrder s2 ON s1.row_id >= b.row_id
GROUP BY s1.column1,s1.columns2;
, 这是递归CTE的工作:
WITH CTE AS (
SELECT
Column1,Column2,rownum = ROW_NUMBER() OVER (ORDER BY (SELECT 1))
FROM someView
),RecCTE AS (
SELECT
Column1,Total = Column1 + Column2
FROM CTE
WHERE rownum = 1
UNION ALL
SELECT
c.Column1,c.Column2,Total = r.Total + c.Column1 + c.Column2
FROM CTE c
INNER JOIN RecCTE ON c.rownum = r.rownum + 1
)
SELECT *
FROM RecCTE
, 您可以像这样分配结果
Declare @X as float
select @X = sum(column1 + column2)
FROM someView
计算此方法有效的运行总计(快速)