Kil Gore
Kil Gore

Reputation: 80

How to shorter SQL function

Is it possible to shortner your SQL function like in C# or Java with lambda.

set serveroutput on;
CREATE OR REPLACE
FUNCTION Two(n IN INT) RETURN FLOAT AS
s FLOAT;
Begin
  s:=0;
  FOR i in 0..n LOOP
    s:=s+1 / POWER(2,i);
  END LOOP;
  
  return s;
END Two;
/
Begin
  DBMS_OUTPUT.PUT_LINE(Two(2));
END;
/

This is how my functions looks like, I feel it's kind of long for the job it does.

How could I shortner it ?

Upvotes: 0

Views: 62

Answers (1)

MT0
MT0

Reputation: 168671

Is it possible to shortner your SQL function like in C# or Java with lambda.

No, there is no lambda syntax in PL/SQL.

How could I shortner it ?

You can just use 2 - 2-n:

CREATE FUNCTION Two(n IN INT) RETURN FLOAT
AS
BEGIN
  RETURN 2 - POWER(2,-n);
END Two;
/

db<>fiddle here

Upvotes: 2

Related Questions