The SYSDATE function is available in both SQL and PL/SQL. They both return the current date-time (down to nearest second) for the database, So it would be reasonable to assume that they "act" the same in both SQL statements and PL/SQL blocks.
That would, however, be a bad assumption to make, because in reality:
In SQL, SYSDATE is called just once for the entire statement.
In PL/SQL, SYSDATE is called every time it is invoked.
Wow. Mind blown.
Let's take a look.
In the script below, I create a table and insert four rows. Then I create a package that keeps track of distinct dates added to a collection and show those dates. Finally, a function that uses the package.
CREATE TABLE tab (id INT)
/
BEGIN
INSERT INTO tab VALUES (1);
INSERT INTO tab VALUES (2);
INSERT INTO tab VALUES (3);
INSERT INTO tab VALUES (4);
COMMIT;
END;
/
CREATE OR REPLACE PACKAGE tracker
AUTHID DEFINER
IS
TYPE when_t IS TABLE OF INTEGER INDEX BY VARCHAR2 (100);
dates when_t;
PROCEDURE add_date (date_in IN DATE);
FUNCTION store_and_return_date (date_in IN DATE,
call_in_plsql_in IN VARCHAR2)
RETURN DATE;
PROCEDURE show_count;
END;
/
CREATE OR REPLACE PACKAGE BODY tracker
IS
PROCEDURE add_date (date_in IN DATE)
IS
BEGIN
dates (TO_CHAR (date_in, 'YYYY-MM-DD HH:MI:SS')) := 0;
END;
PROCEDURE show_count
IS
l_index VARCHAR2 (100) := dates.FIRST;
BEGIN
DBMS_OUTPUT.put_line ('Date Count = ' || dates.COUNT);
WHILE l_index IS NOT NULL
LOOP
DBMS_OUTPUT.put_line (l_index);
l_index := dates.NEXT (l_index);
END LOOP;
dates.delete;
END;
FUNCTION store_and_return_date (date_in IN DATE,
call_in_plsql_in IN VARCHAR2)
RETURN DATE
IS
d DATE;
BEGIN
/* This is the value for SYSDATE passed in from The SELECT */
add_date (date_in);
IF call_in_plsql_in = 'YES'
THEN
/* Now we call SYSDATE inside PL/SQL. Will it be the same
value as passed in for date_in, or different? The
count in the collection will tell us */
add_date (SYSDATE);
END IF;
DBMS_SESSION.sleep (1);
RETURN date_in;
END;
END;
/
When I call the store_and_return_date, I can specify whether or not I want SYSDATE to be called again from within the PL/SQL function - which will be invoked below inside a SELECT statement.SELECT tracker.store_and_return_date (
SYSDATE, call_in_plsql_in => 'NO')
FROM tab
/
BEGIN
tracker.show_count;
END;
/
SELECT tracker.store_and_return_date (
SYSDATE, call_in_plsql_in => 'YES')
FROM tab
/
BEGIN
tracker.show_count;
END;
/
And here's the output:Date Count = 1
2020-09-29 01:28:58
Date Count = 4
2020-09-29 01:29:02
2020-09-29 01:29:03
2020-09-29 01:29:04
2020-09-29 01:29:05
And there you see the difference between SQL and PL/SQL engines when it comes to executing SYSDATE. There are four rows in the TAB table. But the value for SYSDATE when executed in SQL doesn't change for the duration of the query, so there is just one element in the array.
When I ask for SYSDATE to also be called in PL/SQL, it is executed and the current date-time returned - even when that function is called from within a SQL statement.
Why do we have count as 4? Ideally, there is just 2 time insertion into the collection, correct?
ReplyDeleteThe function is called four times. So SYSDATE is executed once in SQL and then four more times inside the PL/SQL function. With the one second delay inside the function, we are (fairly) certain that SYSDATE will return four different date-times, so there are four unique strings used as index values in the collection.
ReplyDeleteDoes that clear things up?
I am caught in the trap here, it is still unclear to me. Sorry, I started to feel silly. When I executed in Toad it returned Count=4. But still when I read it, I don't get it how it returned 4. Can you elaborate it or can you make an other post on this please.
DeleteIn the function: store_and_return_date
--#1. First call to insert into the Collection: dates with parameter's SYSDATE
add_date (date_in);
IF call_in_plsql_in = 'YES'
THEN
--#2.Second call to insert into the Collection: dates, with SYSDATE current
add_date (SYSDATE);
END IF;
Since the table: tab has got 4 rows, what impact does it make while calling functions with parameter (call_in_plsql_in=YES/NO).
I use the parameter call_in_plsql_in to determine if SYSDATE should also be called inside the PL/SQL function. If NO, it is only called in the SQL statement, and we see that it is only actually executed once, since the value passed into the function and used as an index never changes (so the count in the collection is just 1).
DeleteWhen I pass YES for this parameter, SYSDATE is also called inside the PL/SQL function. The collection then ends up with > 1 element in it, because SYSDATE is actually executed each time (with a second's delay in between).
This demonstrates the point of the post:
In a SQL statement, SYSDATE is executed just once and its value is used for the duration of the execution of that statement, no matter how many times SYSDATE is actually "encountered" in the processing of that statement.
In a PL/SQL block, SYSDATE is execute every single time the PL/SQL runtime engine runs across it.
Please don't hesitate to follow up if you need more clarification! This is tricky stuff.....
Thankyou Steven, this helps a lot. I was just lost in the call. Modifying function would explain when it would take 5 Rows into the Collection: dates. Much appreciate your posts. I'm a big fan.
DeleteFUNCTION store_and_return_date (date_in IN DATE,
call_in_plsql_in IN VARCHAR2)
RETURN DATE
IS
d DATE;
BEGIN
add_date (date_in); --First call to insert into the Collection: dates with parameter's SYSDATE
--DBMS_SESSION.sleep (1);
DBMS_LOCK.SLEEP (1);
IF call_in_plsql_in = 'YES'
THEN
add_date (SYSDATE); --Second call to insert into the Collection: dates, with SYSDATE current
END IF;
--DBMS_SESSION.sleep (1);
DBMS_LOCK.SLEEP (1);
RETURN date_in;
END;
Great idea. I have added comments to the code.
Delete