mssql sqlserver 连乘写法分享


摘要:
下文分享一种连乘的写法,如下所示:
实验环境:sql server 2008 R2


例:

    create table #maomao([qty] int)
      insert into #maomao (qty)
     values (1),(2),(3) ,(4),(5),(6),
      (7),(8),(9),(10) 
      go  
  
      ---实现连乘 
        DECLARE @lianCheng int
SET @lianCheng = 1
SELECT  @lianCheng = @lianCheng * ISNULL([qty], 1)
FROM    #maomao
SELECT  @lianCheng as [连乘结果]

      ---实现连加类似 sum用法
             DECLARE @lianJia int
SET @lianJia = 0
SELECT  @lianJia = @lianJia + ISNULL([qty], 0)
FROM    #maomao
SELECT  @ lianJia as [连加结果]

         
      go
      truncate table #maomao
      drop       table  #maomao