0

I want to get count of records in specific partition.
The returned value will be used later in the flow.

DECLARE  
  pt VARCHAR2(20);
  countr NUMBER;
BEGIN
  pt:='61';
   DBMS_OUTPUT.PUT_LINE(pt);

 SELECT COUNT(*) INTO countr FROM T1 PARTITION ( pt) ;

END;

But getting error, it doesn't replace 'pt' with the assigned value.

2
  • You would have to use dynamic SQL to specify a partition name at run time (or a table name, or column name, etc.). But how is the table partitioned - why not filter on the partition range/interval instead? And '61' doesn't look like a valid partition name, as it doesn't meet the object naming rules. Commented Oct 24, 2018 at 12:07
  • 61 is not real name, i wrote from my head... I need to see if partition has data if not, to exit. Commented Oct 24, 2018 at 12:08

1 Answer 1

1

You can't use variables to specify object names (tables, columns, partition etc.) in static SQL.

You need to use dynamic SQL, e.g. with a non-quoted partition name:

DECLARE  
  pt all_tab_partitions.partition_name%TYPE;
  countr NUMBER;
BEGIN
  pt:='p61';
  DBMS_OUTPUT.PUT_LINE(pt);

  EXECUTE IMMEDIATE 'SELECT COUNT(*) FROM T1 PARTITION (' || pt || ')'
  INTO countr ;
END;
/

If you have quoted identifiers for the partition names you need to include the quotes in the statement, and make sure the case of the partition name matches exactly:

DECLARE  
  pt all_tab_partitions.partition_name%TYPE;
  countr NUMBER;
BEGIN
  pt:='61';
  DBMS_OUTPUT.PUT_LINE(pt);

  EXECUTE IMMEDIATE 'SELECT COUNT(*) FROM T1 PARTITION ("' || pt || '")'
  INTO countr ;
END;
/
Sign up to request clarification or add additional context in comments.

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.