Reputation: 1296
I want to use some smart procedures I wrote, but not store them. For example here is this quite big select, that retrieves the primary keys for a table:
SELECT cols.table_name, cols.column_name, cols.position, cons.status, cons.owner
FROM all_constraints cons, all_cons_columns cols
WHERE cols.table_name = 'TABLE_NAME'
AND cons.constraint_type = 'P'
AND cons.constraint_name = cols.constraint_name
AND cons.owner = cols.owner
ORDER BY cols.table_name, cols.position;
I don't want to remember this, not even type it every time I need the constraints of a table. BUT I don't want to create a procedure for this into the DB, because it is a prod DB, and noone else needs my procedure. Just me. Just for developing.
So here is where I am now:
SQL> declare procedure hello as begin dbms_output.put_line('Hello world!'); end; begin hello; end;
2 /
Hello world!
PL/SQL procedure successfully completed.
SQL> hello;
SP2-0042: unknown command "hello" - rest of line ignored.
... so I can declare and define hello function from command line. I can call it from the begin - end block after the declare, but after that the hello procedure is gone.
Question: is there a solution to make the second "hello;" work, so I can create clever and useful procedures and not make any trash in the DB itself?
If this would work, I would create a file with that, and make the alias for sqlplus to execute that file at startup.
Upvotes: 1
Views: 716
Reputation: 191425
You could put each of your queries (are they really procs?) in its own file in your SQLPATH directory, but with positional parameters; e.g. a file called pkeys.sql
:
SELECT cols.table_name, cols.column_name, cols.position, cons.status, cons.owner
FROM all_constraints cons, all_cons_columns cols
WHERE cols.table_name = '&1'
AND cons.constraint_type = 'P'
AND cons.constraint_name = cols.constraint_name
AND cons.owner = cols.owner
ORDER BY cols.table_name, cols.position;
The from SQL*Plus you can just do:
@pkeys TABLE_NAME
If it is a procedure you can still have your declare procedure ... begin ... end
form, and have the call pass &1
to the procedure. Actually, this is pretty much what StevieG was getting at, but with the positional parameter to remove the need to edit the file each time.
Upvotes: 3
Reputation: 8709
You can put the procedure in a script file and run it from the command line using the @ operator. For example
--c:\temp\helloworld.sql
begin
dbms_output.put_line('Hello World');
end;
/
then login to the database and run:
SQL> set serveroutput on
SQL> @c:\temp\helloworld
and you should get:
Hello World
PL/SQL procedure successfully completed.
Upvotes: 3