使用 sqlplus 从命令行运行 oracle 脚本时出现问题
我在尝试使用 sqlplus 将 sql 脚本运行到 oracle 时遇到问题。该脚本只是填充一些虚拟数据:
DECLARE
role1Id NUMBER;
user1Id NUMBER;
role2Id NUMBER;
user2Id NUMBER;
role3Id NUMBER;
user3Id NUMBER;
perm1Id NUMBER;
perm2Id NUMBER;
perm3Id NUMBER;
perm4Id NUMBER;
perm5Id NUMBER;
BEGIN
INSERT INTO PB_USER(USER_ID,USER_NAME, USER_EMAIL, USER_ACTIVEYN)
VALUES(PB_USER_ID_SEQ.nextval, 'RoleDataManagerTests_Username', '[email protected]',' ');
INSERT INTO ROLES(ROLE_ID, ROLE_NAME)
VALUES(PB_ROLE_ID_SEQ.nextval, 'Test role 1');
INSERT INTO ROLES(ROLE_ID, ROLE_NAME)
VALUES(PB_ROLE_ID_SEQ.nextval, 'Test role 2');
INSERT INTO ROLES(ROLE_ID, ROLE_NAME)
VALUES(PB_ROLE_ID_SEQ.nextval, 'Test role 3');
SELECT ROLE_ID INTO role1Id FROM ROLES WHERE ROLE_NAME = 'Test role 1';
SELECT USER_ID INTO user1Id FROM PB_USER WHERE USER_NAME = 'RoleDataManagerTests_Username';
INSERT INTO USERS_ROLES(USER_ID, ROLE_ID) VALUES(user1Id, role1Id);
SELECT ROLE_ID INTO role2Id FROM ROLES WHERE ROLE_NAME = 'Test role 2';
SELECT USER_ID INTO user2Id FROM PB_USER WHERE USER_NAME = 'RoleDataManagerTests_Username';
INSERT INTO USERS_ROLES(USER_ID, ROLE_ID) VALUES(user2Id, role2Id);
SELECT ROLE_ID INTO role3Id FROM ROLES WHERE ROLE_NAME = 'Test role 3';
SELECT USER_ID INTO user3Id FROM PB_USER WHERE USER_NAME = 'RoleDataManagerTests_Username';
INSERT INTO USERS_ROLES(USER_ID, ROLE_ID) VALUES(user3Id, role3Id);
INSERT INTO PERMISSIONS(PERMISSION_ID, KEY, DESCRIPTION)
VALUES (PB_PERMISSION_ID_SEQ.nextval, 'perm1', 'permission 1');
INSERT INTO PERMISSIONS(PERMISSION_ID, KEY, DESCRIPTION)
VALUES (PB_PERMISSION_ID_SEQ.nextval, 'perm2', 'permission 2');
INSERT INTO PERMISSIONS(PERMISSION_ID, KEY, DESCRIPTION)
VALUES (PB_PERMISSION_ID_SEQ.nextval, 'perm3', 'permission 3');
INSERT INTO PERMISSIONS(PERMISSION_ID, KEY, DESCRIPTION)
VALUES (PB_PERMISSION_ID_SEQ.nextval, 'perm4', 'permission 4');
INSERT INTO PERMISSIONS(PERMISSION_ID, KEY, DESCRIPTION)
VALUES (PB_PERMISSION_ID_SEQ.nextval, 'perm5', 'permission 5');
SELECT PERMISSION_ID INTO perm1Id FROM PERMISSIONS WHERE KEY = 'perm1';
SELECT PERMISSION_ID INTO perm2Id FROM PERMISSIONS WHERE KEY = 'perm2';
SELECT PERMISSION_ID INTO perm3Id FROM PERMISSIONS WHERE KEY = 'perm3';
SELECT PERMISSION_ID INTO perm4Id FROM PERMISSIONS WHERE KEY = 'perm4';
SELECT PERMISSION_ID INTO perm5Id FROM PERMISSIONS WHERE KEY = 'perm5';
INSERT INTO ROLES_PERMISSIONS(ROLE_ID, PERMISSION_ID)
VALUES(role1Id, perm1Id);
INSERT INTO ROLES_PERMISSIONS(ROLE_ID, PERMISSION_ID)
VALUES(role1Id, perm2Id);
INSERT INTO ROLES_PERMISSIONS(ROLE_ID, PERMISSION_ID)
VALUES(role1Id, perm3Id);
INSERT INTO ROLES_PERMISSIONS(ROLE_ID, PERMISSION_ID)
VALUES(role2Id, perm3Id);
INSERT INTO ROLES_PERMISSIONS(ROLE_ID, PERMISSION_ID)
VALUES(role3Id, perm4Id);
INSERT INTO ROLES_PERMISSIONS(ROLE_ID, PERMISSION_ID)
VALUES(role3Id, perm5Id);
END;
/
当我使用 Oracle SQL Developer 运行它时,我的脚本工作正常,但是当我使用 sqlplus 命令行工具时,这就是输出的内容,然后它只是挂起:
SQL*Plus: Release 11.1.0.7.0 - Production on Tue May 11 09:49:34 2010
Copyright (c) 1982, 2008, Oracle. All rights reserved.
Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bit Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining Scoring Engine
and Real Application Testing options
我正在使用此命令行运行该工具,这适用于其他脚本:
sqlplus username/password@server/dbname @Setup.sql
有什么想法吗?谢谢。
I'm having a problem trying to run my sql script into oracle using sqlplus. The script just populates some dummy data:
DECLARE
role1Id NUMBER;
user1Id NUMBER;
role2Id NUMBER;
user2Id NUMBER;
role3Id NUMBER;
user3Id NUMBER;
perm1Id NUMBER;
perm2Id NUMBER;
perm3Id NUMBER;
perm4Id NUMBER;
perm5Id NUMBER;
BEGIN
INSERT INTO PB_USER(USER_ID,USER_NAME, USER_EMAIL, USER_ACTIVEYN)
VALUES(PB_USER_ID_SEQ.nextval, 'RoleDataManagerTests_Username', '[email protected]',' ');
INSERT INTO ROLES(ROLE_ID, ROLE_NAME)
VALUES(PB_ROLE_ID_SEQ.nextval, 'Test role 1');
INSERT INTO ROLES(ROLE_ID, ROLE_NAME)
VALUES(PB_ROLE_ID_SEQ.nextval, 'Test role 2');
INSERT INTO ROLES(ROLE_ID, ROLE_NAME)
VALUES(PB_ROLE_ID_SEQ.nextval, 'Test role 3');
SELECT ROLE_ID INTO role1Id FROM ROLES WHERE ROLE_NAME = 'Test role 1';
SELECT USER_ID INTO user1Id FROM PB_USER WHERE USER_NAME = 'RoleDataManagerTests_Username';
INSERT INTO USERS_ROLES(USER_ID, ROLE_ID) VALUES(user1Id, role1Id);
SELECT ROLE_ID INTO role2Id FROM ROLES WHERE ROLE_NAME = 'Test role 2';
SELECT USER_ID INTO user2Id FROM PB_USER WHERE USER_NAME = 'RoleDataManagerTests_Username';
INSERT INTO USERS_ROLES(USER_ID, ROLE_ID) VALUES(user2Id, role2Id);
SELECT ROLE_ID INTO role3Id FROM ROLES WHERE ROLE_NAME = 'Test role 3';
SELECT USER_ID INTO user3Id FROM PB_USER WHERE USER_NAME = 'RoleDataManagerTests_Username';
INSERT INTO USERS_ROLES(USER_ID, ROLE_ID) VALUES(user3Id, role3Id);
INSERT INTO PERMISSIONS(PERMISSION_ID, KEY, DESCRIPTION)
VALUES (PB_PERMISSION_ID_SEQ.nextval, 'perm1', 'permission 1');
INSERT INTO PERMISSIONS(PERMISSION_ID, KEY, DESCRIPTION)
VALUES (PB_PERMISSION_ID_SEQ.nextval, 'perm2', 'permission 2');
INSERT INTO PERMISSIONS(PERMISSION_ID, KEY, DESCRIPTION)
VALUES (PB_PERMISSION_ID_SEQ.nextval, 'perm3', 'permission 3');
INSERT INTO PERMISSIONS(PERMISSION_ID, KEY, DESCRIPTION)
VALUES (PB_PERMISSION_ID_SEQ.nextval, 'perm4', 'permission 4');
INSERT INTO PERMISSIONS(PERMISSION_ID, KEY, DESCRIPTION)
VALUES (PB_PERMISSION_ID_SEQ.nextval, 'perm5', 'permission 5');
SELECT PERMISSION_ID INTO perm1Id FROM PERMISSIONS WHERE KEY = 'perm1';
SELECT PERMISSION_ID INTO perm2Id FROM PERMISSIONS WHERE KEY = 'perm2';
SELECT PERMISSION_ID INTO perm3Id FROM PERMISSIONS WHERE KEY = 'perm3';
SELECT PERMISSION_ID INTO perm4Id FROM PERMISSIONS WHERE KEY = 'perm4';
SELECT PERMISSION_ID INTO perm5Id FROM PERMISSIONS WHERE KEY = 'perm5';
INSERT INTO ROLES_PERMISSIONS(ROLE_ID, PERMISSION_ID)
VALUES(role1Id, perm1Id);
INSERT INTO ROLES_PERMISSIONS(ROLE_ID, PERMISSION_ID)
VALUES(role1Id, perm2Id);
INSERT INTO ROLES_PERMISSIONS(ROLE_ID, PERMISSION_ID)
VALUES(role1Id, perm3Id);
INSERT INTO ROLES_PERMISSIONS(ROLE_ID, PERMISSION_ID)
VALUES(role2Id, perm3Id);
INSERT INTO ROLES_PERMISSIONS(ROLE_ID, PERMISSION_ID)
VALUES(role3Id, perm4Id);
INSERT INTO ROLES_PERMISSIONS(ROLE_ID, PERMISSION_ID)
VALUES(role3Id, perm5Id);
END;
/
My script works fine when I run it using Oracle SQL Developer but when I use the sqlplus command line tool this is what's outputted and then it just hangs:
SQL*Plus: Release 11.1.0.7.0 - Production on Tue May 11 09:49:34 2010
Copyright (c) 1982, 2008, Oracle. All rights reserved.
Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bit Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining Scoring Engine
and Real Application Testing options
I'm running the tool using this command line, which works fine for other scripts:
sqlplus username/password@server/dbname @Setup.sql
Any ideas? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
发布评论
评论(6)
上面提到的命令行
sqlplus username/password@server/dbname @Setup.sql
意味着sqlplus
应该执行脚本Setup.sql
并以交互方式等待进一步的命令(如果脚本没有明确执行exit
) 。这是sqlplus
的正常行为。
sqlplus
在三种情况下终止:
- 失败(对于某些错误,您可以更改它是否终止。请参阅
WHENEVER
plsql-command) - 显式
exit
(交互和脚本) - STDIN 结束 (EOF)
无论是从交互模式还是从脚本,您都可以发送 ^Z
字符来软终止输入流。以交互方式,您只需按 Ctrl+Z,Enter
即可。
当然,您可以重定向 STDIN,并从文件而不是键盘中获取它。有两种类似的方法可以执行此操作:
1) sqlplus username/password@server/dbname<Setup.sql
2) echo @Setup.sql|sqlplus username/password@server/dbname
在这两种情况下,sqlplus
都会在脚本执行后终止,因为输入流中存在 EOF
。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
您需要在脚本末尾添加
exit
,或者以sqlplus username/password@server/dbname
运行它。 Setup.sql
(即重定向输入,<
而不是@
)。您只需在挂起会话中输入“exit”即可检查这是否是问题所在。如果确实挂起,您是否已从 Developer 提交或回滚了执行?
You need to either put an
exit
at the end of the script, or run it assqlplus username/password@server/dbname < Setup.sql
(i.e. redirected input,<
instead of@
). You can check if that's the issue by just typing 'exit' in the hanging session.If it is really hanging, have you committed or rolled back the execution from Developer?