如何创建oracle程序但不存储

发布于 2025-01-01 12:03:13 字数 968 浏览 1 评论 0原文

我想使用我编写的一些智能程序,但不存储它们。例如,这是一个相当大的选择,它检索表的主键:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

何以心动 2025-01-08 12:03:13

您可以将每个查询(它们真的是过程吗?)放在 SQLPATH 目录中自己的文件中,但带有位置参数;例如,一个名为 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;

从 SQL*Plus 中您可以执行以下操作:

@pkeys TABLE_NAME

如果它是一个过程,您仍然可以使用您的 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:

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.

灼痛 2025-01-08 12:03:13

您可以将该过程放入脚本文件中并使用 @ 运算符从命令行运行它。例如

--c:\temp\helloworld.sql

begin
  dbms_output.put_line('Hello World');
end;
/

,然后登录数据库并运行:

SQL> set serveroutput on
SQL> @c:\temp\helloworld

您应该得到:

Hello World

PL/SQL procedure successfully completed.

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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文