How to replace tablename with a variable in a DB2 cursor in an anonymous block

StackOverflow https://stackoverflow.com/questions/17085498

  •  31-05-2022
  •  | 
  •  

سؤال

I want to replace the table name with a variable that is derived from another cursor, but no matter what logic I tried to use I just could not get it right, I am seeing a couple of examples for Oracle and SQL Server but I failed to interpret that code into the DB2 SQL. Please help.

Declare
    v_user VarCHAR(100);
    v_schema VARCHAR(1000);
    V_Studio_svr VARCHAR(1000);
    v_db2_schema VARCHAR(1000);
    v_oracle_string varchar(5000) ;
    v_db2_string varchar(5000) := '(' ;
    v_sys_columns varchar(2000);
    v_sys_values  varchar(2000);
    V_UID iNTEGER := 41;



begin
-- Main Table to Get Table Name From 
    FOR v In ( Select app_id,Upper(alias) ALIAS 
               From FREEDOM.FORMS where app_id = '5e988af8-ef0f-48c7-9794-9bc4f1134c80' ) Loop
      v_schema := 'S__'||V.app_ID||'_1';
      v_schema := replace(v_schema,'-','_');
      v_studio_svr := 'PTU'||SUBSTR(v.alias,2,LENGTH(v.alias));
      v_db2_schema := 'TF'||SUBSTR(v.alias,2,LENGTH(v.alias));
    -- This is where I want to use Table Name as Variable Coming From Cursor V
      For P in 
        (Select * from studio_svr||'.'||v_studio_svr) loop  
     -- Table to get Data Type Mappings 
         For i in 
            (Select * From fREEDOM.DB2_DT_MAPPING
             Where Table_Name = v.alias )  Loop

                   IF I.DB2_DATATYPE LIKE 'DECIMAL%' THEN
                    v_ORACLE_STRING := Nvl(v_ORACLE_STRING,'')||'CAST('||'INTEGER('||I.STUDIO_SVR_COLUMN||') AS DECIMAL(22,6)),';
               ELSE
                 v_ORACLE_STRING := Nvl(v_ORACLE_STRING,'')||I.STUDIO_SVR_COLUMN||',';
               END IF;
                     v_DB2_STRING := v_DB2_STRING||I.DB2_COLUMN||',';

         End Loop;  

         v_DB2_STRING := SUBSTR(v_DB2_STRING,1,LENGTH(v_DB2_STRING)-1)||')';

  execute immediate 'Insert Into ' || v_schema || '.' || v_db2_schema || '  '|| v_db2_string ||' SELECT '|| v_oracle_string ||' FROM Studio_svr.' || v_studio_svr || 'where S__recordid ='||p.s__recordid ;

   v_db2_string := '(';
   v_oracle_string := '';
   v_uid := v_uid + 1;
   commit;
   End loop;
  END lOOP;

END

هل كانت مفيدة؟

المحلول

Obviously, you need to use dynamic SQL for that cursor, like so:

  Declare
      v_user VarCHAR(100);
      ...
      V_UID iNTEGER := 41;

    --->
      v_cursor_studio SYS_REFCURSOR;

  begin
  -- Main Table to Get Table Name From 
      FOR v In ( Select app_id,Upper(alias) ALIAS 
                 From FREEDOM.FORMS where app_id = '5e988af8-ef0f-48c7-9794-9bc4f1134c80' ) Loop
        v_schema := 'S__'||V.app_ID||'_1';
        v_schema := replace(v_schema,'-','_');
        v_studio_svr := 'PTU'||SUBSTR(v.alias,2,LENGTH(v.alias));
        v_db2_schema := 'TF'||SUBSTR(v.alias,2,LENGTH(v.alias));
      -- This is where I want to use Table Name as Variable Coming From Cursor V

      --->
        OPEN v_cursor_studio for 'Select * from ' || studio_svr||'.'||v_studio_svr;
        For P in v_cursor_studio
           ...

The code is not tested, but I hope you get the idea.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top