Unable to run dynamic query in stored procedure while selecting count of records

[亡魂溺海] 提交于 2019-12-11 18:51:11

问题


Hi I am trying to simply get the count of data in source table.

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;

I am getting

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

Can any one help me on this ?

I am trying to code a procedure where i can test if all the data are completely moved from source to target table.


回答1:


Your dynamic statement should not have a semicolon at the end; that is a statement separator and not relevant or valid for a single statement. You can only run a single SQL statement dynamically anyway (unless you put several into an anonymous PL/SQL block).

Your into is in the wrong place too:

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;

Not sure why you're bothering to have and assign local variables when you can use the procedure arguments directly, which I think makes the statement more readable:

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


来源:https://stackoverflow.com/questions/24260571/unable-to-run-dynamic-query-in-stored-procedure-while-selecting-count-of-records

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!