我正在将一些存储过程从Sybase TSQL转换为Oracle PL / SQL,而且我已经遇到了一个我很难解决的问题!
以下代码无法运行:
DECLARE
t INT := 0;
t_error EXCEPTION;
v_line VARCHAR2(100);
BEGIN
SELECT COUNT(*) INTO t FROM user_tables WHERE table_name = UPPER('tbl_BSUK_PriceIssue');
IF t = 1 THEN
EXECUTE IMMEDIATE 'DROP TABLE tbl_BSUK_PriceIssue';
t := 0;
SELECT COUNT(*) INTO t FROM user_tables WHERE table_name = UPPER('tbl_BSUK_PriceIssue');
IF t = 1 THEN
RAISE t_error;
END IF;
END IF;
EXCEPTION
WHEN t_error THEN
v_line := '<<< FAILED DROPPING table tbl_BSUK_PriceIssue >>>';
dbms_output.put_line (v_line);
WHEN OTHERS THEN
v_line := '<<< Unknown Error >>>';
dbms_output.put_line (v_line);
END;
END;
我收到以下错误消息,我做错了什么?!
在命令行中从第17行开始出错 - DECLARE
t INT:= 0; t_error EXCEPTION; v_line VARCHAR2(100);
BEGIN
SELECT COUNT(*)INTO t FROM user_tables WHERE table_name = UPPER(&#39; tbl_BSUK_PriceIssue&#39);
IF t = 1然后立即执行&#39; DROP TABLE tbl_BSUK_PriceIssue&#39;;
t:= 0; SELECT COUNT(*)INTO t FROM user_tables WHERE table_name = UPPER(&#39; tbl_BSUK_PriceIssue&#39);如果t = 1那么 提升t_error;万一;结束如果;t_error然后v_line:=&#39;&lt;&lt;&lt; FAILED DROPPING表 tbl_BSUK_PriceIssue&gt;&gt;&gt;&#39 ;; dbms_output.put_line(v_line);什么时候 其他那么 v_line:=&#39;&lt;&lt;&lt;未知错误&gt;&gt;&gt;&#39 ;; dbms_output.put_line(v_line);结束; 结束;错误报告 - ORA-06550:第30行,第1列:PLS-00103:遇到符号&#34; END&#34; 06550. 00000 - &#34;行%s,列%s:\ n%s&#34; *原因:通常是PL / SQL编译错误。 *操作:
我实际上是尝试用PL / SQL版本替换以下TSQL:
-- Create temp table for relevant trev_id's
IF OBJECT_ID('dbo.tbl_BSUK_PriceIssue') IS NOT NULL
BEGIN
DROP TABLE dbo.tbl_BSUK_PriceIssue
IF OBJECT_ID('dbo.tbl_BSUK_PriceIssue') IS NOT NULL
PRINT '<<< FAILED DROPPING TABLE dbo.tbl_BSUK_PriceIssue >>>'
ELSE
PRINT '<<< DROPPED TABLE dbo.tbl_BSUK_PriceIssue >>>'
END
go
答案 0 :(得分:9)
尝试删除本节中的END;
WHEN OTHERS THEN
v_line := '<<< Unknown Error >>>';
dbms_output.put_line (v_line);
END;
UPD。实际上,你可以做得更短,不需要在删除后检查表是否存在
declare
eTableNotExists exception;
pragma exception_init(eTableNotExists, -00942);
begin
EXECUTE IMMEDIATE 'DROP TABLE tbl_BSUK_PriceIssue';
dbms_output.put_line('<<< DROPPED TABLE dbo.tbl_BSUK_PriceIssue >>>');
exception
when eTableNotExists then null
when others then
dbms_output.put_line ('<<< Unknown Error >>>' || sqlerrm);
end;
/
答案 1 :(得分:1)
我不知道错误,但您可以在一小部分代码中执行您想要的操作。如果使用EXISTS()
并且不需要EXECUTE IMMEDIATE
,则不需要count变量,因为命令中没有可变数据:
IF 1 = (SELECT 1 FROM user_tables WHERE table_name = 'TBL_BSUK_PRICEISSUE') THEN
DROP TABLE tbl_BSUK_PriceIssue;
IF 1 = (SELECT 1 FROM user_tables WHERE table_name = 'TBL_BSUK_PRICEISSUE') THEN
RAISE EXCEPTION;
END IF;
END IF;