Oracle uses a cursor to query a string that combines the names of all fields in a specified data table.

Source: Internet
Author: User

Oracle uses a cursor to query a string that combines the names of all fields in a specified data table.

Application Scenario: refer to the Code for querying all field names of a data table on the Internet, and use a cursor to generate a list of field names separated by select commas (,) consisting of all field names and commas (,) of a specified table.

The query result is output as follows:

The field list string of the current data table TB_UD_USER is

The detailed script code is as follows:


Declare

Cursor mycursor is -- defines the cursor

Select distinct TABLE_COLUMN. *, TABLE_NALLABLE.DATA_TYPE, TABLE_NALLABLE.NULLABLE from (select distinct utc. table_name, utc. comments table_comments, ucc. column_name, ucc. comments column_comments from user_tab_comments utc, user_col_comments ucc where utc. table_name = ucc. table_name and utc. table_name not like '% _ B' and utc. table_name not like '% _ Z' and utc. table_name not like '% 1%') TABLE_COLUMN, (select distinct table_name, column_name, nullable, DATA_TYPE from user_tab_cols where table_name not like '% _ B' and table_name not like '% _ Z' and table_name not like' % 100 ') TABLE_NALLABLE where TABLE_COLUMN.column_name = tables and TABLE_COLUMN.TABLE_NAME = TABLE_NALLABLE.table_name and TABLE_COLUMN.TABLE_NAME = mytablename order by TABLE_COLUMN.TABLE_NAME, TABLE_COLUMN.column_name;

If mycursor % found then -- the cursor's found attribute determines whether a record exists

End;

Else

Begin

Selstring: = 'select' | mystring | 'from' | mytablename;

Dbms_output.put_line ('current data table '| mytablename | 'Statement for querying all records is ');

Exit;

End;

End if;

Close mycursor;

End;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.