Billy McBurnie Billy McBurnie - 1 year ago 70
SQL Question

SQL Solve a Function with a While Loop

Using SQL server, write a while loop to calculate and print the functional value of f(x) = 3x&3 -2X^2 + 15 for all odd non negative integers <= 25. ( 1, 3, 5, …23,25).

This is what I have written out so far

My Query

And this is the result I get

My Results

So theoretically I should be getting 45,640 when

@x = 25

What am I going wrong here?

Answer Source

You have to use power function like the following

Declare @SUMSQUARE Integer, @x Integer
SET @X = 1

WHILE (@X <= 25)

SELECT @SUMSQUARE =  (3*power(@X,3))-(2*power(@X,2))+15
SET @X = @X + 2

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download