选择记录计数时,无法在存储过程中运行动态查询

时间:2014-06-17 09:46:00

标签: sql oracle stored-procedures oracle-sqldeveloper

您好我想简单地获取源表中的数据计数。

CREATE OR REPLACE PROCEDURE MOVE_CHECK (SCHEMA_SOURCE   IN VARCHAR2,
                                        SCHEMA_TARGET   IN VARCHAR2,
                                        TABLE_SOURCE    IN VARCHAR2,
                                        TABLE_TARGET    IN VARCHAR2,
                                        COLUMN_SOURCE   IN VARCHAR2,
                                        COLUMN_TARGET   IN VARCHAR2)
AS
   A              VARCHAR2 (30);
   B              VARCHAR2 (30);
   C              VARCHAR2 (30);
   D              VARCHAR2 (30);
   E              VARCHAR2 (30);
   F              VARCHAR2 (30);
   Count_source   NUMBER (38);
   TEMP_1         VARCHAR2 (100);
BEGIN
   A := SCHEMA_SOURCE;
   B := SCHEMA_TARGET;
   C := TABLE_SOURCE;
   D := TABLE_TARGET;
   E := COLUMN_SOURCE;
   F := COLUMN_TARGET;

   TEMP_1 :=
      'select count ( ' || E || ' ) into  Count_source   from ' || C || ';';

   DBMS_OUTPUT.PUT_LINE ('STARTED');
   DBMS_OUTPUT.PUT_LINE (TEMP_1);

   EXECUTE IMMEDIATE (TEMP_1);

   DBMS_OUTPUT.PUT_LINE ('source_count:' || Count_source);

   DBMS_OUTPUT.PUT_LINE ('END');
END MOVE_CHECK;

我正在

Connecting to the database 
ORA-00911: invalid character
ORA-06512: at "YDSCST.MOVE_CHECK", line 31
ORA-06512: at line 16

任何人都可以帮我吗?

我正在尝试编写一个程序,我可以测试所有数据是否完全从源表移动到目标表。

1 个答案:

答案 0 :(得分:1)

你的动态陈述最后不应该有分号;这是一个语句分隔符,对单个语句不相关或无效。您无论如何都只能动态运行单个SQL语句(除非您将多个SQL语句放入匿名PL / SQL块中)。

您的into也在错误的位置:

TEMP_1 := 'select count ( '|| E ||' ) from ' || C;

DBMS_OUTPUT.PUT_LINE ('STARTED');
DBMS_OUTPUT.PUT_LINE (TEMP_1);

EXECUTE IMMEDIATE TEMP_1 INTO Count_source;

当你可以直接使用过程参数时,不确定你为何要烦恼并分配局部变量,我认为这使得语句更具可读性:

TEMP_1 := 'select count ( '|| COLUMN_SOURCE ||' ) from ' || TABLE_SOURCE;