如何创建oracle程序但不存储
我想使用我编写的一些智能程序,但不存储它们。例如,这是一个相当大的选择,它检索表的主键:
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;
我不想记住这一点,甚至不想每次需要表的约束时都键入它。但我不想为此在数据库中创建一个过程,因为它是一个产品数据库,而且没有其他人需要我的过程。只有我。只为发展。
所以这就是我现在所在的位置:
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.
...所以我可以从命令行声明和定义 hello 函数。我可以在声明之后从 begin - end 块调用它,但之后 hello 过程就消失了。
问题:有没有办法让第二个“hello;”工作,这样我就可以创建聪明且有用的程序,并且不会在数据库本身中产生任何垃圾?
如果这可行,我将用它创建一个文件,并为 sqlplus 创建别名以在启动时执行该文件。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将每个查询(它们真的是过程吗?)放在 SQLPATH 目录中自己的文件中,但带有位置参数;例如,一个名为
pkeys.sql
的文件:从 SQL*Plus 中您可以执行以下操作:
如果它是一个过程,您仍然可以使用您的
declare procedure ... begin ... end 形式,并将调用传递
&1
到过程。实际上,这几乎就是 StevieG 的目的,但是使用位置参数来消除每次编辑文件的需要。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 ... 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.您可以将该过程放入脚本文件中并使用 @ 运算符从命令行运行它。例如
,然后登录数据库并运行:
您应该得到:
You can put the procedure in a script file and run it from the command line using the @ operator. For example
then login to the database and run:
and you should get: