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.
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:The from SQL*Plus you can just do:
If it is a procedure you can still have your
declare procedure ... begin ... endform, and have the call pass&1to 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.