0

While running this prepared statement

DECLARE

BEGIN
-- Get the list of dependent tables and store it in a variable.
FOR cons IN (SELECT A.TABLE_NAME 
    FROM ALL_CONSTRAINTS A, ALL_CONSTRAINTS B
    WHERE A.CONSTRAINT_TYPE = 'xxx'
    AND A.R_CONSTRAINT_NAME = B.CONSTRAINT_NAME
    AND A.R_OWNER  = B.OWNER
    AND B.TABLE_NAME = 'MY_TABLE'
    AND B.OWNER = 'DBA')

LOOP
    SET @querytext = CONCAT('SELECT * FROM ',cons.TABLE_NAME);

        PREPARE stamquery FROM @querytext;

        EXECUTE stamquery;

        DEALLOCATE PREPARE stamquery;

END LOOP;
END;

I am getting the error stating:

Encountered the symbol "STAMQUERY" when expecting one of the following:

   := . ( @ % ;

I am new with procedures but looks like this is the way to do it based on my research on the internet.

Please let me know what am I doing wrong..

2
  • SET @querytext = ... is invalid PL/SQL. Where in the manual did you find tha? Commented Oct 30, 2017 at 22:36
  • What exactly you want to do? Commented Oct 31, 2017 at 7:48

1 Answer 1

1

The Syntax you are using for PL/SQL is not correct. It should be something like this.

DECLARE
  querytext   VARCHAR2(1000); --declare the variable to store query.
  v_tab_count NUMBER;
BEGIN
  -- Get the list of dependent tables and store it in a variable.
  FOR cons IN
  (
    SELECT
      A.TABLE_NAME
    FROM
      ALL_CONSTRAINTS A,
      ALL_CONSTRAINTS B
    WHERE
      A.CONSTRAINT_TYPE     = 'xxx'
    AND A.R_CONSTRAINT_NAME = B.CONSTRAINT_NAME
    AND A.R_OWNER           = B.OWNER
    AND B.TABLE_NAME        = 'MY_TABLE'
    AND B.OWNER             = 'DBA'
  )
  LOOP
    querytext := CONCAT('SELECT COUNT(*) FROM ',cons.TABLE_NAME);
    EXECUTE IMMEDIATE querytext INTO v_tab_count ; --dynamicall execute the
    -- select statement.
    DBMS_OUTPUT.PUT_LINE( 'TABLE ' ||cons.TABLE_NAME||' COUNT '||v_tab_count);
    -- Display the table name and its count of rows.
  END LOOP;
END;
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome Thanks :)

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.