1

I have below anonymous block and here before execute immediate str string is failing due to invalid relational operator error.

not getting why.

SET SERVEROUT ON ;

DECLARE
   SRCE_SB_CATGRY_ID   VARCHAR2 (1000);
BEGIN
   FOR i
      IN (SELECT ISO_CNTRY_ID, X.COLUMN_NAME
            FROM iso_cntry I,
                 (SELECT DISTINCT COLUMN_NAME
                    FROM ALL_tab_cols@suplr_md_qa2
                   WHERE     table_name = 'TEMP_ARIBA'
                         AND COLUMN_NAME != 'SSC_ID') x
           WHERE ISO_CNTRY_DESC_TXT LIKE '' || COLUMN_NAME || '%')
   LOOP
      DECLARE
         str1   VARCHAR2 (10000)
            :=    ' SELECT DISTINCT ssc_id  '
               || ' FROM stddata_suplr.temp_ariba@suplr_md_qa2 '
               || ' WHERE '
               || ' '
               || i.column_name
               || ' = ''Y'' ';
         x      SYS_REFCURSOR;
      BEGIN
         OPEN x FOR str1;

         LOOP
            FETCH x INTO SRCE_SB_CATGRY_ID;

            EXIT WHEN x%NOTFOUND;



            NULL;
         END LOOP;
      END;
   END LOOP;

   END;
3
  • 3
    I don't see any obvious problem. One possibility is that one of your column names has characters that require it to be enclosed in double-quotes. I suggest you add DBMS_OUTPUT.PUT_LINE(str1) before opening the cursor, so you can see exactly what SQL you are trying to execute. Commented Nov 18, 2020 at 16:38
  • Hi Dave, Many thanks for your response on this , i already did that which you are suggesting , but no result , i executed the same using execute immediate but same error . Commented Nov 18, 2020 at 16:42
  • Hi Belayer , its throwing invalid relational operator , when i am passing hardcoded value instead of i.column_name in str then its working , but not working with i.column_name , i placed this string in dbms_output , and all generated statements are valid ... Commented Nov 18, 2020 at 18:10

3 Answers 3

0

You should not use the declare section twice in anonymous block.

Try this:

SET SERVEROUT ON ;

DECLARE
   SRCE_SB_CATGRY_ID   VARCHAR2 (1000);
   str1   VARCHAR2 (10000);
   x      SYS_REFCURSOR;
BEGIN
   FOR i
      IN (SELECT ISO_CNTRY_ID, X.COLUMN_NAME
            FROM iso_cntry I,
                 (SELECT DISTINCT COLUMN_NAME
                    FROM ALL_tab_cols@suplr_md_qa2
                   WHERE     table_name = 'TEMP_ARIBA'
                         AND COLUMN_NAME != 'SSC_ID') x
           WHERE ISO_CNTRY_DESC_TXT LIKE '' || COLUMN_NAME || '%')
   LOOP
         str1 :=    ' SELECT DISTINCT ssc_id  '
               || ' FROM stddata_suplr.temp_ariba@suplr_md_qa2 '
               || ' WHERE '
               || ' '
               || i.column_name
               || ' = ''Y'' ';
         
         OPEN x FOR str1;

         LOOP
            FETCH x INTO SRCE_SB_CATGRY_ID;

            EXIT WHEN x%NOTFOUND;



            NULL;
         END LOOP;
      END;
   END LOOP;

   END;

Please manage the logic according to your requirement.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi Tejas , Thanks for your suggestion , but its stil showing the same error.
Remove everything except str1 assignment and check if there is an error in dynamic query itself.
0

This line in the driving looks wrong:

WHERE ISO_CNTRY_DESC_TXT LIKE '' || COLUMN_NAME || '%'

Try it without concatenating the leading quote:

WHERE ISO_CNTRY_DESC_TXT LIKE COLUMN_NAME || '%'

1 Comment

Hi Apc , placed your suggestion in the script , not working still throwing the same error
0

Many thanks for all your suggestions. It was a data error for one of the iso_cntry_id . I filtered it in my where clause and it's working fine now.

Changes which I made in first loop

FOR i
      IN (SELECT ISO_CNTRY_ID, X.COLUMN_NAME
            FROM iso_cntry I,
                 (SELECT DISTINCT COLUMN_NAME
                    FROM ALL_tab_cols@suplr_md_qa2
                   WHERE table_name = 'TEMP_ARIBA') x
           WHERE     ISO_CNTRY_DESC_TXT LIKE '' || COLUMN_NAME || '%'
                 AND i.ISO_CNTRY_ID != 18)
   LOOP

best regards, Vijay

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.