输入@n INT = 5如何找到系列的总和?
应该产生系列作为1,2,3,4,5
预期结果:应显示总和1 + 2 + 3 + 4 + 5 = 15
总和= 15
我怎么可以这样使用SQL Server
输入@n INT = 5如何找到系列的总和?
应该产生系列作为1,2,3,4,5
预期结果:应显示总和1 + 2 + 3 + 4 + 5 = 15
总和= 15
我怎么可以这样使用SQL Server
DECLARE @n INT = 5;
WITH num(i) AS
(
SELECT 1 AS i
UNION ALL
SELECT i+1 AS i FROM num WHERE i <= @n - 1
)
SELECT SUM(i) FROM num
declare @n int = 5;
declare @sum int;
set @sum = @n * (@n + 1)/2;
select @sum
Declare @n int = 5
Declare @o int=0
while @n>=1
begin
set @[email protected][email protected]
set @[email protected]
End
select @o
试试这个吧!
declare @n int
set @n=5
declare @sum1 int
set @sum1=0
declare @i int
set @i=0
print 'the Series is'
while @i<@n
begin
print @i+1
set @[email protected]+1
set @[email protected][email protected]
end
print 'the sum is'
print @sum1
如果你只是想总结,然后用公式试试这个代码
declare @n int
set @n= 5;
declare @sum int;
set @sum = @n * (@n + 1)/2;
select @sum
'N *(N + 1)/ 2' – AbhinavRanjan