Use cursors in Oracle to get strings for all field names in the specified data table

Source: Internet
Author: User

Operation steps: After opening Plsql developer, directly execute the following statement can come out


Use cursors in--oracle to get strings for all field names in the specified data table

Declare
Mytablename VARCHAR (255): = ' Staffdoc '; --Define the data table name variable to query, staffdoc the data table name for my test, please modify it to the corresponding data table name in your database.
MyString NVARCHAR2 (4000): = "; --Define the string variables to output

Cursor MyCursor is--defines a cursor
SELECT * from All_tab_columns where Table_name=mytablename ORDER by column_id;
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 Length (mystring) >0 Then
mystring:=mystring| | ', ' | | Myrecord.column_name;
Else
Mystring:=myrecord.column_name;
End If;
End

Else
Exit
End If;

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

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.