Wednesday, October 10, 2018

API Script to fetch Table Structure from Backend in Oracle


Please Note that this script will fetch only Table Structure but not Primary Key, Foreign Keys, Unique Keys, Indexes..etc.

DECLARE
   x_table_header      VARCHAR2 (50);
   x_data_type_row   VARCHAR2 (100);
   x_data_type           VARCHAR2 (100);
   tab_count               NUMBER := 0;
   x_column_id          NUMBER := 0;

   CURSOR tab
   IS
      SELECT ao.object_name
        FROM all_objects ao
       WHERE ao.object_type = 'TABLE'
         AND ao.object_name = 'XX_TEST1';

   CURSOR cols (p_table_name VARCHAR2)
   IS
        SELECT *
          FROM all_tab_columns atc
         WHERE atc.table_name = p_table_name
      ORDER BY column_id;
BEGIN
   DBMS_OUTPUT.enable (10000000);

   FOR i IN tab
   LOOP
      tab_count := tab_count + 1;
      x_table_header := 'CREATE TABLE ' || i.object_name || ' (';

      DBMS_OUTPUT.PUT_LINE (x_table_header);

      FOR j IN cols (i.object_name)
      LOOP
         SELECT MAX (atc.column_id)
           INTO x_column_id
           FROM all_tab_columns atc
          WHERE atc.table_name = i.object_name;

         IF j.column_id <> x_column_id
         THEN
            SELECT    j.data_type
                   || DECODE (j.data_type,
                              'VARCHAR2', '(' || j.data_length || '),',
                              ',')
              INTO x_data_type
              FROM dual;

            x_data_type_row :=
               RPAD (j.column_name, 30, ' ') || '      ' || x_data_type;
         ELSIF j.column_id = x_column_id
         THEN
            SELECT    j.data_type
                   || DECODE (j.data_type,
                              'VARCHAR2', '(' || j.data_length || '));',
                              ');')
              INTO x_data_type
              FROM dual;

            x_data_type_row :=
               RPAD (j.column_name, 30, ' ') || '      ' || x_data_type;
         END IF;

         DBMS_OUTPUT.PUT_LINE (x_data_type_row);
      END LOOP;

      DBMS_OUTPUT.PUT_LINE (' ');
   END LOOP;
END;

No comments:

Post a Comment

SupplierAddressImportTemplate.xlsm South Africa Suburb Field mapping in POZ_SUPPLIER_ADDRESSES_INT

Suburb mpping in Supplier Address Import Template will be mapped to Address Element Attribute2 (HZ_LOCATIONS. ADDR_ELEMENT_ATTRIBUTE2)