【问题标题】:SQL Solve a Function with a While LoopSQL 用 While 循环求解函数
【发布时间】:2017-04-04 04:39:01
【问题描述】:

使用 SQL 服务器,编写一个 while 循环来计算并打印 f(x) = 3x&3 -2X^2 + 15 对于所有奇数非负整数

这是我目前写的内容

这就是我得到的结果

所以理论上我应该得到 45,640 @x = 25

我这里出了什么问题?

【问题讨论】:

    标签: sql sql-server tsql while-loop


    【解决方案1】:

    您可以使用递归 cte(而不是使用 while 循环)来生成所有

    with oddnums_cte as (select 1 num
                         union all
                         select num+2 from oddnums_cte where num < 25)
    select num, (3*power(num,3))-(2*power(num,2))+15 as function_value
    from oddnums_cte
    

    【讨论】:

      【解决方案2】:

      您必须使用power 函数,如下所示

      Declare @SUMSQUARE Integer, @x Integer
      SET @X = 1
      
      WHILE (@X <= 25)
      BEGIN
      
      SELECT @SUMSQUARE =  (3*power(@X,3))-(2*power(@X,2))+15
      PRINT @SUMSQUARE
      SET @X = @X + 2
      
      END
      

      【讨论】:

        猜你喜欢
        • 2021-12-03
        • 2021-01-14
        • 1970-01-01
        • 1970-01-01
        • 2014-03-29
        • 2013-04-03
        • 2014-03-11
        • 2020-03-01
        • 1970-01-01
        相关资源
        最近更新 更多