1

下面是确定表是否存在然后删除/创建表的 PL/SQL 脚本。如果它不存在,无论如何都要创建表。因此,当我运行脚本时,我收到以下信息:

ORA-00904: "E": 无效标识符

我认为语法是准确的,双引号的“E”是正确的,但显然不是。请指教。

谢谢。

DECLARE 

  l_cnt NUMBER;

BEGIN
  SELECT COUNT(*)
    INTO l_cnt
    FROM dba_tables
   WHERE owner = 'ABCD'
     AND table_name = 'SEC_REC_TEMP';

  IF( l_cnt > 0 )
  THEN
     EXECUTE IMMEDIATE 'DROP TABLE sec_rec_temp';
     EXECUTE IMMEDIATE 'CREATE TABLE sec_rec_temp 
                        AS
                        SELECT is.inv_num
                          FROM abcd.inv_summ is
                              ,abcd.bill_fee bf
                         WHERE is.inv_num = bf.inv_num
                           AND trim(bf.fee_type) = "E"
                         GROUP BY is.inv_num';
  ELSE
     EXECUTE IMMEDIATE 'CREATE TABLE sec_rec_temp 
                        AS
                        SELECT is.inv_num
                          FROM abcd.inv_summ is
                              ,abcd.bill_fee bf
                         WHERE is.inv_num = bf.inv_num
                           AND trim(bf.fee_type) = "E"
                         GROUP BY is.inv_num';
  END IF;
END;
/ 
4

2 回答 2

3

首先,删除和重新创建表的 PL/SQL 脚本非常可疑。例如,您真的确定不想创建一个只需定期刷新的物化视图吗?在 PL/SQL 中执行 DDL 几乎总是一种糟糕的方法。如果您可以解释您要解决的业务问题,我们可能会为您提供更好的技术解决方案。

其次,如果要在PL/SQL中对字符串中的单引号进行转义,则需要使用两个连续的单引号。不是一个双引号字符。

DECLARE 
  l_cnt NUMBER;
BEGIN
  SELECT COUNT(*)
    INTO l_cnt
    FROM dba_tables
   WHERE owner = 'ABCD'
     AND table_name = 'SEC_REC_TEMP';

  IF( l_cnt > 0 )
  THEN
     EXECUTE IMMEDIATE 'DROP TABLE sec_rec_temp';
     EXECUTE IMMEDIATE 'CREATE TABLE sec_rec_temp 
                        AS
                        SELECT is.inv_num
                          FROM abcd.inv_summ is
                              ,abcd.bill_fee bf
                         WHERE is.inv_num = bf.inv_num
                           AND trim(bf.fee_type) = ''E''
                         GROUP BY is.inv_num';
  ELSE
     EXECUTE IMMEDIATE 'CREATE TABLE sec_rec_temp 
                        AS
                        SELECT is.inv_num
                          FROM abcd.inv_summ is
                              ,abcd.bill_fee bf
                         WHERE is.inv_num = bf.inv_num
                           AND trim(bf.fee_type) = ''E''
                         GROUP BY is.inv_num';
  END IF;
END;

或者,您可以使用 q 引用语法

DECLARE 
  l_cnt NUMBER;
BEGIN
  SELECT COUNT(*)
    INTO l_cnt
    FROM dba_tables
   WHERE owner = 'ABCD'
     AND table_name = 'SEC_REC_TEMP';

  IF( l_cnt > 0 )
  THEN
     EXECUTE IMMEDIATE 'DROP TABLE sec_rec_temp';
     EXECUTE IMMEDIATE q'[CREATE TABLE sec_rec_temp 
                        AS
                        SELECT is.inv_num
                          FROM abcd.inv_summ is
                              ,abcd.bill_fee bf
                         WHERE is.inv_num = bf.inv_num
                           AND trim(bf.fee_type) = 'E'
                         GROUP BY is.inv_num]';
  ELSE
     EXECUTE IMMEDIATE q'[CREATE TABLE sec_rec_temp 
                        AS
                        SELECT is.inv_num
                          FROM abcd.inv_summ is
                              ,abcd.bill_fee bf
                         WHERE is.inv_num = bf.inv_num
                           AND trim(bf.fee_type) = 'E'
                         GROUP BY is.inv_num]';
  END IF;
END;
于 2013-02-08T23:21:10.317 回答
2

您需要用另一个单引号转义单引号

  AND trim(bf.fee_type) = ''E''
于 2013-02-08T23:22:33.307 回答