0

How do I declare a variable in PL/SQL that is a table? I don't want a new table in the database. I want a variable that is declared at the beginning of my session and is automatically destroyed at the end.

I have created the following custom types obj_gifts & tbl_gifts

Create Or Replace Type obj_gifts as object (gifts_key number, gift_name vachar(100));

Create Or Replace Type tbl_gifts Is Table Of obj_gifts


I want something akin to the following

Declare gifts tbl_gifts

Insert Into gifts Values (1,'toy')

Select * from gifts


What I DO NOT WANT is to create a new table named gifts just to run my query.

8
  • 2
    This may not be a thing you can do. Do you need to use the object and table types you've declared? You could create a private temporary table but you can't create a private temporary table based on an object type. On the other hand, your insert statement wouldn't be valid if gifts was defined based on an object type. Commented May 5, 2023 at 21:12
  • @JustinCave thank you for the suggestion. I talked to my IT department. A private temp table is off the table. They will not grant permission for people to create private temp tables. They suggested a global temp which they do allow. Why? No idea. A global temp won't work however for two reasons. 1. It's a f**king idiotic solution. 2. The purpose of this is to write something reusable that multiple people can run concurrently without collision. Managing a global temp with concurrency would be a nightmare. Commented May 8, 2023 at 14:23
  • What exactly do you want to be dynamic? For example, if the object type is dynamic, that wouldn't work with multiple users running the script at the same time. You could declare a record type in PL/SQL and a local collection of that record type in PL/SQL and that will be completely private. But you'd generally need to use that in PL/SQL not directly in SQL. Commented May 8, 2023 at 14:40
  • @JustinCave thank you for asking. I admit I am an old hand at T-SQL but new to Oracle. The SQL<->PL/SQL distinction is over my head. The types obj_gifts and tbl_gifts are resident and persistent in the database. The dynamic variable is gifts and should be private to each individua executing the code. For example if developers A & B execute the code concurrently from different computers they will both have an instance of the variable "gifts" that is of type tbl_gifts. Does that make sense in Oracle-land? In T-SQL this is dead easy no special handling required or concerns about concurrency. Commented May 9, 2023 at 15:25
  • @KevinWhite You appear to have an XY-problem; you are trying to do something (we don't quite know what) but the solution you would implement in SQL Server is to use variables and you are fixated on implementing that solution in Oracle SQL. That is probably not going to lead to an optimal Oracle solution (and is going to be difficult to implement as you are trying to do something that is not natural in Oracle) and it would probably be better to ask about how to solve the underlying problem.
    – MT0
    Commented May 10, 2023 at 14:17

1 Answer 1

4

You don't need to create a type.

Just use the DUAL table if you want a query to output those values:

SELECT 1 AS gifts_key, 'toy' AS gift_name FROM DUAL;

If you did want to create a type then:

SELECT *
FROM   TABLE(tbl_gifts(obj_gifts(1, 'toy')));

And, if you want to use PL/SQL then:

DECLARE
  gifts tbl_gifts;
BEGIN
  gifts := tbl_gifts();
  gifts.EXTEND;
  gifts(gifts.COUNT) := obj_gifts(1, 'toy');

  FOR i IN 1 .. gifts.COUNT LOOP
    DBMS_OUTPUT.PUT_LINE(gifts(i).gifts_key || ', ' || gifts(i).gift_name);
  END LOOP;
END;
/

I want a variable that is declared at the beginning of my session and is automatically destroyed at the end.

That sounds like you may want a table; a private temporary table.

CREATE PRIVATE TEMPORARY TABLE ORA$PTT_GIFTS
(
  gifts_key number,
  gift_name varchar2(100)
)
  ON COMMIT PRESERVE DEFINITION;

Then you can insert into the table, select from it and it will be destroyed at the end of the session.

fiddle

1
  • Thanks @MT0. Corporate IT won't allow private temp tables (don't ask). A variable assignment may be an option. Commented May 8, 2023 at 14:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.