Oracle uses a cursor to query a string that is a combination of all the field names of a specified data table

Source: Internet
Author: User

Application: Refer to all the field name codes of the data table on the web, use cursors to generate all field names of the specified single table followed by commas for the list of field names separated by the select comma from the string and other occasions.

The query result output is as follows:

The field list string for the current data table Tb_ud_user is
Ah,birthplace,bm,cellphone,cjgzrq,department2,djjid,gzcx,gzkh,gzsfzh,hjdz,hyzk,id,jhrq,jtzz,lafx_ld,ljdz,lly, Lxdh,name,nxdh,policenumber,reserve1,reserve10,reserve9,sccp,sex,sfjh,sfqby,sfz,spjb,yl_22,zj,zw,zzmm
The current data table Tb_ud_user queries all record statements as
Select Ah,birthplace,bm,cellphone,cjgzrq,department2,djjid,gzcx,gzkh,gzsfzh,hjdz,hyzk,id,jhrq,jtzz,lafx_ld,ljdz , lly,lxdh,name,nxdh,policenumber,reserve1,reserve10,reserve9,sccp,sex,sfjh,sfqby,sfz,spjb,yl_22,zj,zw,zzmm from Tb_ud_user

The detailed script code is as follows:


--oracle A string that uses a cursor to query all the field names of a specified data table

Declare
Mytablename NVARCHAR2 (+): = ' tb_ud_user '; --Define the data table name variable to query
MyString NVARCHAR2 (1000): = "; --Define a list of field names to output string variables
Selstring VARCHAR2 (2000): = "; --Define the query statement string variables to be output

Cursor MyCursor is--Define cursors   

SELECT DISTINCT table_column.*,table_nallable. Data_type,table_nallable. nullable  from (SELECT DISTINCT utc.table_name  table_name, utc.comments    table_comments, Ucc.column_name 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 don't like '%1% ') table_column,       (select DISTINCT table_name, COLUMN_NAME, nullable, data_type from user_tab_cols  where table_name don't like '%_b '   and T Able_name not like '%_z '   and table_name don't like '%1% ') table_nallable  where table_column.column_name = Table_ nallable.column_name   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;


Myrecord Mycursor%rowtype; --Define Cursor record type
Counter int: = 0;
Begin
Open mycursor; --Open cursor
If Mycursor%isopen then--Judging open success
Loop-Loop Get recordset
Fetch mycursor into Myrecord; --Get the record in the cursor

If Mycursor%found then--the found property of the cursor determines if there is a record
Begin
--if it is the first field
if (mystring= ") Then
Mystring:=myrecord.column_name;
Else
mystring:=mystring| | ', ' | | Myrecord.column_name;
End If;

End

Else

Begin

Dbms_output.put_line (' Current data table ' | | mytablename| | ' The field list string is ');
Dbms_output.put_line (mystring);

selstring:= ' SELECT ' | | mystring| | ' From ' | | Mytablename;

Dbms_output.put_line (' Current data table ' | | mytablename| | ' Query all record statements as ');
Dbms_output.put_line (selstring);

Exit

End

End If;

End Loop;
Else
Dbms_output.put_line (' Cursor not open ');
End If;

Close MyCursor;

End

Oracle uses a cursor to query a string that is a combination of all the field names of a specified data table

Related Article

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.