SQL变量,怎么做?

时间:2012-01-09 10:15:34

标签: sql oracle variables

我有这个SQL:

DROP TABLE MISSINGTABLE;

CREATE TABLE MISSINGTABLE (
  TABLE_NAME VARCHAR2 (70),
  DESCRIPTION VARCHAR2 (1000)
)

CREATE OR REPLACE PROCEDURE MISSINGTABLES AS
  BEGIN
      INSERT INTO MISSINGTABLE
      ((((SELECT TABLE_NAME, 'Missing Table on PEKA_ERP_001' Description FROM ALL_TABLES WHERE OWNER = 'ASE_ERP_001')
      MINUS
      (SELECT TABLE_NAME, 'Missing Table on PEKA_ERP_001' Description FROM ALL_TABLES WHERE OWNER = 'PEKA_ERP_001'))
        UNION
      ((SELECT TABLE_NAME, 'Missing Table on ASE_ERP_001' Description FROM ALL_TABLES WHERE OWNER = 'PEKA_ERP_001')
      MINUS
      (SELECT TABLE_NAME, 'Missing Table on ASE_ERP_001' Description FROM ALL_TABLES WHERE OWNER = 'ASE_ERP_001'))));
  END;

所以,你怎么看,我正在创建一个表,然后是一个填充表的过程。 现在我想要 2个变量用于这些参数'PEKA_ERP_001''ASE_ERP_001'(所以我并不总是需要手动编写它,因为这个值会发生很大的变化)

我试过这个(仅包含上述Statement 的第一部分):

DECLARE
  S1 VARCHAR2(100) := 'ASE_ERP_001';
  S2 VARCHAR2(100) := 'PEKA_ERP_001';
  TableMissing VARCHAR(100) := 'Missing Table on ';
  Apostrophe VARCHAR(10) := '''';
  BEGIN
      EXECUTE IMMEDIATE ('CREATE OR REPLACE PROCEDURE MISSINGTABLES AS BEGIN INSERT INTO MISSINGTABLE (SELECT TABLE_NAME, ' || Apostrophe || TableMissing || S2 || Apostrophe || ' Description FROM ALL_TAB_COLUMNS WHERE OWNER = ' || Apostrophe || S1 || Apostrophe || ')' || ' END;');
END;

它创建了The Procedure,但是Procedure包含了“CREATE OR REPLACE PROCEDURE”本身并且它显示了一个错误......(我无法执行该过程)

任何人都可以帮助我吗? 如何在头部编写第一个SQL语句,只有2个变量ASE_ERP_001PEKA_ERP_001

编辑:

说明:

 DECLARE
  S1 VARCHAR2(100) := 'ASE_ERP_001';
  S2 VARCHAR2(100) := 'PEKA_ERP_001';
  TabelleFehlt VARCHAR(100) := 'Diese Tabelle fehlt ';
  Hochkomma VARCHAR(10) := '''';
  BEGIN
      EXECUTE IMMEDIATE ('CREATE OR REPLACE PROCEDURE MISSINGTABLES AS BEGIN INSERT INTO MISSINGTABLE (SELECT TABLE_NAME, ' || Hochkomma || TabelleFehlt || S2 || Hochkomma || ' Beschreibung FROM ALL_TAB_COLUMNS WHERE OWNER = ' || Hochkomma || S1 || Hochkomma || ') END;');
END;

上述声明创建程序。 但它也向我展示了这一点:

    ORA-06512: in Row 7
    24344. 00000 -  "success with compilation error"
    *Cause:    A sql/plsql compilation error occurred.
    *Action:   Return OCI_SUCCESS_WITH_INFO along with the error code

程序本身包含这个:

    create or replace
PROCEDURE MISSINGTABLES AS BEGIN INSERT INTO MISSINGTABLE (SELECT TABLE_NAME, 'Diese Tabelle fehlt PEKA_ERP_001' Beschreibung FROM ALL_TAB_COLUMNS WHERE OWNER = 'ASE_ERP_001') END;

但它不应该包含“创建或替换过程失误”等只有INSERT STatement,我无论如何都无法执行过程。

更好的方法是使用bpgergo中的脚本,如果可以的话。

3 个答案:

答案 0 :(得分:4)

我希望我没有混淆这些论点,你应该再次检查它们

CREATE OR REPLACE PROCEDURE MISSINGTABLES (p_1 in varchar2, p_2 in varchar2)
AS
  BEGIN
      INSERT INTO MISSINGTABLE
      ((((SELECT TABLE_NAME, 'Missing Table on '||p_1 Description FROM ALL_TABLES WHERE OWNER = p_2)
      MINUS
      (SELECT TABLE_NAME, 'Missing Table on '||p_1 Description FROM ALL_TABLES WHERE OWNER = p_1))
        UNION
      ((SELECT TABLE_NAME, 'Missing Table on '||p_2 Description FROM ALL_TABLES WHERE OWNER = p_1)
      MINUS
      (SELECT TABLE_NAME, 'Missing Table on '||p_2 Description FROM ALL_TABLES WHERE OWNER = p_2))));
END;

修改

你会这样称呼:

begin
    MISSINGTABLES ('PEKA_ERP_001', 'ASE_ERP_001');
end;

答案 1 :(得分:1)

您尝试立即执行的SQL将被评估为:

CREATE OR REPLACE PROCEDURE MISSINGTABLES AS 
BEGIN 
INSERT INTO MISSINGTABLE 
(SELECT TABLE_NAME, COLUMN_NAME, 'Missing Table on PEKA_ERP_001' Beschreibung 
 FROM ALL_TAB_COLUMNS WHERE OWNER = 'ASE_ERP_001')
END;

这可能不是您真正想要的逻辑,但直接的问题是您正在尝试填充一个名为Beschreibung的不存在的第三列,而不是填充第二列DESCRIPTION

答案 2 :(得分:1)

我可以建议改进你的SELECT吗?

这是一个可能的选择:

SELECT
  TABLE_NAME,
  'Missing Table on'
  || CASE MAX(OWNER) WHEN 'PEKA_ERP_001' THEN 'ASE_ERP_001' ELSE 'PEKA_ERP_001' END
  AS Description
FROM ALL_TABLES
WHERE OWNER IN ('PEKA_ERP_001', 'ASE_ERP_001')
GROUP BY TABLE_NAME
HAVING COUNT(*) = 1

此查询仅返回TABLE_NAME只有一个OWNER的行。然后,显示缺少该表的所有者将被视为正在测试的两个中的另一个。

使用参数,整个CREATE PROCEDURE语句可能如下所示:

CREATE OR REPLACE PROCEDURE MISSINGTABLES
(
  owner1 IN varchar2,
  owner2 IN varchar2
)
AS
BEGIN
  INSERT INTO MISSINGTABLE
  (
  SELECT
    TABLE_NAME,
    'Missing Table on'
    || CASE MAX(OWNER) WHEN owner1 THEN owner2 ELSE owner1 END
    AS Description
  FROM ALL_TABLES
  WHERE OWNER IN (owner1, owner2)
  GROUP BY TABLE_NAME
  HAVING COUNT(*) = 1
  );
END;