Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle Functions that Take No Parameters

Why is it that CURRENT_DATE, CURRENT_TIMESTAMP, SYSDATE, and SYSTIMESTAMP are called without parentheses. I understand that they take no parameters, but in other languages, you would still call the functions using parentheses. Is it the case in Oracle that any function that never takes parameters cannot be called with parentheses?

If someone could point me to the documentation on this, I'd appreciate it.

like image 738
Webucator Avatar asked Jun 08 '19 23:06

Webucator


People also ask

Can function have out parameter in Oracle?

A function can have OUT or IN OUT parameters, but this is bad coding practice.

How do you call a function with out parameters in SQL?

1) You cannot call a PROCEDURE from SQL only a function. You have the above defined as a function but it doesn't return anything sensible. 2) you cannot call a function that has an OUT parameter -- only in parameters are allowed in SQL. Your solution is easy -- use a function that returns the number.

Is Empty function in Oracle?

IS EMPTY function. The IS EMPTY function provides an alternative syntax to IS_EMPTY and also returns TRUE if that set is empty. where set is a set of any set data type, such as a multi-assign double attribute. The results of this example would the same as the previous IS_EMPTY example.

What are the restrictions in functions in Oracle?

The stored function may not modify database tables. It cannot execute an INSERT, DELETE, or UPDATE statement. A stored function that is called remotely or through a parallelized action may not read or write the values of package variables. The Oracle Server does not support side effects that cross user sessions.


3 Answers

Oracle is weird in many ways. It plays fast and loose with lots of things: the meaning of NULL, implicit data type conversions, and a whole number of other things.

Among them, as you noticed, is their inconsistent syntax for calling functions with no parameters. (For declaring such functions, too - see below.)

Native functions like sysdate and current_timestamp, which do not take arguments, must be written without parentheses. You ask for documentation... the most direct (and yet not entirely satisfactory) pointer is to the documentation of each function, where the syntax is shown very clearly without parentheses. What are you looking for - a separate mention in the documentation, where they state this explicitly?

Compare this with analytic functions like rownumber(), for example, which also do not take arguments. You must write them with empty parentheses!

Worse: For functions you write yourself in PL/SQL, and call from SQL statements: if the function takes no arguments, then it must be defined without parentheses. However, when you invoke it (in a select statement, for example) you can call it with or without (empty) parentheses - both syntaxes are valid. Not so, alas, with the native functions, like sysdate. Why? Again, a good question to ask Oracle.

It doesn't end there, either. connect_by_root is a "hierarchical function" (a function that can be used in hierarchical queries). It takes an argument - which can be given in parentheses (as in any normal function) or without parentheses! Go figure.

If you ask WHY??? - you are not alone. I have no clue either.

like image 182
mathguy Avatar answered Sep 28 '22 00:09

mathguy


Those are all Pseudocolumns which are used in SQL or PL/SQL statements like columns but they aren't really stored on the disk. They can be thought of as special-purpose data elements within the SQL statements just as if they were part of the table.

A DML statement neither be applied on Pseudocolumns, nor user-defined they are, so they do not need to be considered as standard functions or procedures, and the style is conventionally defined by Oracle, itself.

By the way, this syntax(without parentheses) is valid even for a function in Oracle provided that no needs to have any parameter as seems more logical rather than what other systems do.

Oracle says : Parameter declarations are optional. Functions that take no parameters are written without parentheses

like image 39
Barbaros Özhan Avatar answered Sep 27 '22 22:09

Barbaros Özhan


To further confuse things, some SQL functions have corresponding PL/SQL functions defined in the "STANDARD" PL/SQL package. This allows functions like SYSDATE to be invoked on the right hand side of an assignment statement in a PL/SQL program block. Because the function in the "STANDARD" package is a PL/SQL function (and not an SQL one), it can be invoked with or without parentheses.

DECLARE
  x DATE;
BEGIN
  x:= SYSDATE();  -- Valid
  x:= SYSDATE;    -- Also valid
END;
like image 45
cdub Avatar answered Sep 27 '22 23:09

cdub