Oracle SQLPlus问题-将字符串写入txt文件

发布于 2024-11-25 10:49:11 字数 2243 浏览 1 评论 0原文

我正在尝试将 sql 命令写入 sql 文件,以便输出本身可以用作脚本。基本上使用脚本来创建本身格式化为可运行脚本的输出。也许这是多余的,但我还没有想到更好的方法。如果可能的话,我可以使用额外的眼睛(或想法)。谢谢

这是脚本:

CONNECT &&master_user/&&master_pwd.@&&tns_alias
SET LINESIZE 132 PAGESIZE 0 ECHO OFF FEEDBACK OFF
SET VERIFY OFF HEAD OFF TERM OFF TRIMSPOOL ON
SPO syns_list.sql
--GRANT CREATE ANY SYNONYM TO &&syn_user;//how is this line formed?
select  'create or replace synonym ' || table_name ||
' for ' || '&&master_user..' ||
table_name || ';'
from user_tables
order by table_name asc;
--REVOKE CREATE ANY SYNONYM FROM &&syn_user;//how is this line formed?
SPO OFF;
SET ECHO ON FEEDBACK ON VERIFY ON HEAD ON TERM ON;
exit

这是输出:

//need the "grant" line here
create or replace synonym AGENTS for webdemo_admin.AGENTS;
create or replace synonym CONSTRUCTION_COMPANY for webdemo_admin.CONSTRUCTION_COMPANY;
create or replace synonym CONTRACT for webdemo_admin.CONTRACT;
create or replace synonym CUSTOMERS for webdemo_admin.CUSTOMERS;
create or replace synonym CUSTOMER_INTEREST for webdemo_admin.CUSTOMER_INTEREST;
create or replace synonym FEATURE for webdemo_admin.FEATURE;
create or replace synonym HOME for webdemo_admin.HOME;
create or replace synonym HOME_NONSTD_FEATURE for webdemo_admin.HOME_NONSTD_FEATURE;
create or replace synonym INTEREST for webdemo_admin.INTEREST;
create or replace synonym NON_STD_FEATURE for webdemo_admin.NON_STD_FEATURE;
create or replace synonym SALES for webdemo_admin.SALES;
create or replace synonym STD_FEATURE for webdemo_admin.STD_FEATURE;
create or replace synonym STD_MODEL for webdemo_admin.STD_MODEL;
create or replace synonym STD_MODEL_FEATURE for webdemo_admin.STD_MODEL_FEATURE;
create or replace synonym SUB_CONTRACTOR for webdemo_admin.SUB_CONTRACTOR;
create or replace synonym WARRANTY_WORK for webdemo_admin.WARRANTY_WORK;
//need the "revoke" line here

我确信有更好的方法可以做到这一点,我仍在不断地学习。作为解释(为了理解):我希望维护一个对表(和架构)具有完全控制权的用户,但我需要一个具有上面列出的私有同义词的用户(或最多 10 个用户)才能进行多个连接具有与房地产经纪人相同类型的访问权限(从 Web 应用程序的角度查看此内容时)。也就是说,同义词允许选择、插入、更新和删除事务。在同义词创建之前和之后进行授予/撤销的原因只是为了使该过程自动化并消除授予普通用户创建任何同义词的安全问题。此外,我不希望公开同义词,因为这也是一个令人头疼的跟踪问题(也是一个安全问题)。无论如何,私有更好并且提供更多控制。

再次,我感谢任何人提出的任何建议。

I am attempting to write an sql command into an sql file so that the output itself can be used as a script. Basically using a script to create output that is itself formatted into runnable script. Maybe it's redundant but I haven't been able to think of a better way. I could use an extra set of eyes (or ideas) if possible. Thanks

Here's the script:

CONNECT &&master_user/&&master_pwd.@&&tns_alias
SET LINESIZE 132 PAGESIZE 0 ECHO OFF FEEDBACK OFF
SET VERIFY OFF HEAD OFF TERM OFF TRIMSPOOL ON
SPO syns_list.sql
--GRANT CREATE ANY SYNONYM TO &&syn_user;//how is this line formed?
select  'create or replace synonym ' || table_name ||
' for ' || '&&master_user..' ||
table_name || ';'
from user_tables
order by table_name asc;
--REVOKE CREATE ANY SYNONYM FROM &&syn_user;//how is this line formed?
SPO OFF;
SET ECHO ON FEEDBACK ON VERIFY ON HEAD ON TERM ON;
exit

This is the output:

//need the "grant" line here
create or replace synonym AGENTS for webdemo_admin.AGENTS;
create or replace synonym CONSTRUCTION_COMPANY for webdemo_admin.CONSTRUCTION_COMPANY;
create or replace synonym CONTRACT for webdemo_admin.CONTRACT;
create or replace synonym CUSTOMERS for webdemo_admin.CUSTOMERS;
create or replace synonym CUSTOMER_INTEREST for webdemo_admin.CUSTOMER_INTEREST;
create or replace synonym FEATURE for webdemo_admin.FEATURE;
create or replace synonym HOME for webdemo_admin.HOME;
create or replace synonym HOME_NONSTD_FEATURE for webdemo_admin.HOME_NONSTD_FEATURE;
create or replace synonym INTEREST for webdemo_admin.INTEREST;
create or replace synonym NON_STD_FEATURE for webdemo_admin.NON_STD_FEATURE;
create or replace synonym SALES for webdemo_admin.SALES;
create or replace synonym STD_FEATURE for webdemo_admin.STD_FEATURE;
create or replace synonym STD_MODEL for webdemo_admin.STD_MODEL;
create or replace synonym STD_MODEL_FEATURE for webdemo_admin.STD_MODEL_FEATURE;
create or replace synonym SUB_CONTRACTOR for webdemo_admin.SUB_CONTRACTOR;
create or replace synonym WARRANTY_WORK for webdemo_admin.WARRANTY_WORK;
//need the "revoke" line here

I am sure there are better ways to do this, I'm still picking things up as I go. As fo an explanation (for understanding): I want that I maintain a user with full control over the tables (and the schema) but I need a user (or upto 10 users) with the above-listed private synonyms in order for multiple connections to have the same type of access that a real-estate agent might (when viewing this from the perspective of a web app). That is, the synonyms allow for select,insert, update and delete transactions. The reason for the grant/revoke before and after the synonym creation is only to automate the process and remove the security problem of granting the create any synonym to a normal user. Further, I do not want the synonyms to be public as that is also a headache to keep track of (and again a security problem). In any case, private is better and offers more control.

Again, I appreciate any suggestions anyone may have.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

旧人哭 2024-12-02 10:49:11

SQL*Plus PROMPT 关键字是您想要的吗?这仅显示该行的其余部分:

SQL> DEFINE syn_user = exampleuser1234
SQL> PROMPT GRANT CREATE ANY SYNONYM TO &&syn_user;;
GRANT CREATE ANY SYNONYM TO exampleuser1234;
SQL>

请注意,我们必须加倍分号,因为 SQL*Plus 将第一个分号解释为替换参数 syn_user 的结尾。

Is the SQL*Plus PROMPT keyword what you're after? This just displays the rest of the line:

SQL> DEFINE syn_user = exampleuser1234
SQL> PROMPT GRANT CREATE ANY SYNONYM TO &&syn_user;;
GRANT CREATE ANY SYNONYM TO exampleuser1234;
SQL>

Note that we must double the semicolon since SQL*Plus interprets the first one as the end of the substitution parameter syn_user.

小…楫夜泊 2024-12-02 10:49:11

如果您要以同义词所有者的身份运行生成的脚本(情况似乎如此),则不需要 CREATE ANY SYNONYM 权限,只需 CREATE SYNONYM 权限。我只会在创建用户时授予该权限。

要在此脚本中进行授权,您需要首先以特权用户(例如 SYSTEM)的身份进行连接。在这种情况下,您不妨将整个系统作为 SYSTEM 来运行,它已经具有CREATE ANY SYNONYM。为此,您只需修改创建语句以包含同义词的所有者,例如创建或替换同义词.STD_MODEL_FEATURE for webdemo_admin.STD_MODEL_FEATURE;(其中< syn_user>&syn_user 替换变量的值)。

(澄清一下,这是两种不同的处理方法。选择其中之一,而不是两者都选。)

If you're going to run the generated script as the synonym owner, which appears to be the case, it shouldn't need CREATE ANY SYNONYM privilege, just CREATE SYNONYM privilege. I would just grant that privilege when creating the user.

To do a grant in this script, you'd need to first connect as a privileged user, such as SYSTEM. In that case, you might as well run the whole things as SYSTEM, which already has CREATE ANY SYNONYM. To do that you would just need to modify your create statements to include the owner of the synonym, e.g. create or replace synonym <syn_user>.STD_MODEL_FEATURE for webdemo_admin.STD_MODEL_FEATURE; (where <syn_user> is the value of the &syn_user substitution variable).

(To clarify, those are two different ways of approaching it. Pick one or the other, not both.)

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