我目前正在编写一个SQL脚本,在数据库中进行了一些更改,我希望用户确认每个步骤的结果。
代码:
SET ECHO ON
SET AUTOCOMMIT OFF
SET EXITCOMMIT OFF
-- make changes
update plan_table
set statement_id = '3'
where statement_id = '2';
-- check if correct
select statement_id from plan_table;
-- ask confirmation
ACCEPT response CHAR DEFAULT 'n' PROMPT 'Result ok? - y/n: ';
if lower(&response) = 'y' then (
update plan_table
set statement_id = '4'
where statement_id = '3';
-- check if correct
select statement_id from plan_table;
-- ask confirmation
ACCEPT response CHAR DEFAULT 'n' PROMPT 'Result ok? - y/n: ';
if lower(&response) = 'y' then commit;
else rollback;
end if;
)
else rollback;
end if;
/
exit;
更改有效,但我在尝试验证时总是收到错误(接受响应部分):
SQL> -- ask confirmation
SQL> ACCEPT response CHAR DEFAULT 'n' PROMPT 'Result ok? - y/n: ';
Result ok? - y/n: SQL> if lower(&response) = 'y' then commit;
SP2-0734: unknown command beginning "if lower(&..." - rest of line ignored.
SQL> else rollback;
SP2-0734: unknown command beginning "else rollb..." - rest of line ignored.
SQL> end if;
SP2-0042: unknown command "end if" - rest of line ignored.
SQL> )
SP2-0042: unknown command ")" - rest of line ignored.
SP2-0044: For a list of known commands enter HELP
and to leave enter EXIT.
SQL> else rollback;
SP2-0734: unknown command beginning "else rollb..." - rest of line ignored.
SQL> end if;
SP2-0042: unknown command "end if" - rest of line ignored.
SQL> /
谁能告诉我我做错了什么?
错误:
Enter value for userreply: old 7: case &userReply when 'y' then exit commit
new 7: case y when 'y' then exit commit
ACCEPT userReply PROMPT 'Result ok? - y/n: '
*
ERROR at line 6:
ORA-06550: line 6, column 8:
PLS-00103: Encountered the symbol "USERREPLY" when expecting one of the
following:
:= . ( @ % ;
由于
答案 0 :(得分:4)
好的,作为一项有趣的练习,我已经在SQL脚本中实现了您的要求。我不推荐这种方法,它过于复杂而且坦率地说有点傻!最好的方法是在您选择的开发环境中编写应用程序 - 可以是APEX,Forms,ASP,Pro * C,......
我必须创建一些脚本:
1)do_update.sql:
-- make changes
update plan_table
set statement_id = '&1.'
where statement_id = '&2.';
-- check if correct
select statement_id from plan_table;
-- ask confirmation
ACCEPT response CHAR DEFAULT 'n' PROMPT 'Result ok? - y/n: ';
-- Based on confirmation set prefix to 'do' or 'dont'
set term off verify off
column prefix new_value prefix
select case upper('&response.')
when 'Y' then 'do'
else 'dont'
end as prefix
from dual;
set term on
2)dont_update.sql:
-- don't make changes
3)do_commit.sql:
commit;
4)dont_commit.sql:
rollback;
5)main.sql:
@@do_update 3 2
@@&prefix._update 4 3
@@&prefix._commit
现在在SQL Plus中运行main.sql:
SQL> @main
1 row updated.
STATEMENT_ID
------------------------------
1
3
3
Result ok? - y/n: y
2 rows updated.
STATEMENT_ID
------------------------------
1
4
4
Result ok? - y/n: y
Commit complete.
当然,这只是基于您的简单示例。对于更现实的情况,脚本的数量会更大。我会不这样做!
答案 1 :(得分:1)
首先,您使用pl / sql语法编写。 sql中没有条件子句。尝试纠正语法并开始用pl / sql语法编写。 据我所知,逻辑很清楚。