Description of all fields in a specified data table read by SQL

Source: Internet
Author: User

-- SQL reads all fields in a specified data table
Declare @ tablename nvarchar (250) -- Name of the data table in the current operation
Set @ tablename = 'hzfa '-- specify the name of the data table to be queried (please change it to the name of the table in your own database)
Declare @ columnname nvarchar (250) -- Name of the field currently queried in the cursor
Declare @ columndescription nvarchar (250) -- Description of the fields currently queried in the cursor

-- Declare the cursor for reading all fields in the data table
Declare mycursor cursor for select. name, cast (G. value as nvarchar) from sys. columns A left join sys. extended_properties g on (. object_id = G. major_id and. column_id = G. minor_id) Where object_id = object_id (''+ @ tablename +'') order by object_id,. column_id
-- Open the cursor
Open mycursor
-- Retrieve the data from the cursor and assign values to the constraint name variable.
Fetch next from mycursor into @ columnname, @ columndescription
-- If the cursor is successfully executed
While (@ fetch_status = 0)
Begin
If (@ columndescription is null)
Begin
Print 'the description of the current data table ['+ @ tablename +'] field ['+ @ columnname +'] is blank'
End
Else
Begin
-- Query the field description currently found
The description of the print 'current data table ['+ @ tablename +'] field ['+ @ columnname +'] is ['+ @ columndescription +'] '.
End

-- Use a cursor to retrieve the next record
Fetch next from mycursor into @ columnname, @ columndescription
End
-- Close the cursor
Close mycursor
-- Undo cursor
Deallocate mycursor

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.