SQL reads the names and default values of all constraints for all data tables in the database.

Source: Internet
Author: User

-- Original design intention: Convert the data table of the simplified version to the traditional version
-- Query all user data tables with a cursor
Declare @ currenttablename nvarchar (250)
Declare usertablecursor cursor for -- declare a cursor
Select name from sysobjects where xtype = 'U' and Category = '000000' order by name
Open usertablecursor -- open the cursor
Fetch next from usertablecursor -- get the next row of the cursor
-- Make the variable get the name of the row specified by the current cursor
Into @ currenttablename
While @ fetch_status = 0 -- the fetch statement is successfully executed.
Begin
-- Query all constraints in the current data table
Print n 'current data table name' + @ currenttablename
-- Use a cursor to query constraints in a specified user data table
Declare @ constrainttype nvarchar (250)
Declare @ constraintname nvarchar (250)
Declare @ constraintvalue nvarchar (250)
Declare constraintcursor cursor for select. xtype as constraint type,. name as constraint name, isnull (B. text, N 'null') as default value from sysobjects a left Outer Join syscomments B on. id = B. ID where (objectproperty (. ID, N 'ismsshipped ') = 0) and. parent_obj = (select ID from sysobjects where name = @ currenttablename)
Open constraintcursor -- open the cursor
Fetch next from constraintcursor -- get the next row of the cursor
-- Make the variable get the name of the row specified by the current cursor
Into @ constrainttype, @ constraintname, @ constraintvalue
While @ fetch_status = 0 -- the fetch statement is successfully executed.
Begin
-- Query all constraints in the current data table
Print n 'current data table name' + @ currenttablename + N' constraint type '+ @ constrainttype + N' constraint name' + @ constraintname + N' default value' + @ constraintvalue

Fetch next from constraintcursor -- get the next row of the cursor
-- Obtain the name, cyclebegintime, and cycleendtime of the row specified by the current cursor.
Into @ constrainttype, @ constraintname, @ constraintvalue
End
Close constraintcursor -- close the cursor
Deallocate constraintcursor -- release cursor

Fetch next from usertablecursor -- get the next row of the cursor
-- Obtain the name, cyclebegintime, and cycleendtime of the row specified by the current cursor.
Into @ currenttablename
End
Close usertablecursor -- close the cursor
Deallocate usertablecursor -- release cursor

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.