SQL SERVER避免我们在SQL函数中将paramter作为输出“通过引用传递”传递 它只允许我们在程序中这样做 有没有办法在函数中做到这一点?
如果没有,请任何人帮我在Select语句中分配值
就像这个例子:
Declare @X as float
set @X
select column1, column2, @X = @X+ column1+column2 FROM someView
答案 0 :(得分:1)
要产生这样的结果,您可以尝试这个解决方案:
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, s1.column2, 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;
答案 1 :(得分:0)
您可以像这样分配结果
Declare @X as float
select @X = sum(column1 + column2)
FROM someView
计算运行总计this approach的作品(快速)
答案 2 :(得分:0)
这是递归CTE的工作:
WITH CTE AS (
SELECT
Column1,
Column2,
rownum = ROW_NUMBER() OVER (ORDER BY (SELECT 1))
FROM someView
),
RecCTE AS (
SELECT
Column1,
Column2,
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