Statements that list all fields with default values in SQL Server

Source: Internet
Author: User

Expert answers
By querying three system tables in any database, you can obtain the default values of each field in each table. The following is the core query. It returns the default value allocated to each user table in the current database. This query is compatible with SQL 2000 and SQL 2005.

Quote: CopyCode The Code is as follows: Select so. Name as "table name", SC. Name as "column name", Sm. Text as "Default Value"
From DBO. sysobjects so inner join DBO. syscolumns SC on so. ID = SC. ID
Left join DBO. syscomments SM on SC. cdefault = Sm. ID
Where so. xtype = 'U'
Order by so. [name], SC. colid

Sysobjects provides table metadata for us. In this example, we are only interested in table names. The syscolumns table stores metadata associated with each field of each table. In this example, we only need the field name. Finally, the default metadata is provided by the syscomments table.
Run this query on the northwind database to generate the following results (for the sake of simplicity, some records are omitted ). Note: Because left joins the syscomments table, it returns the default value of null.

Now I am thinking about the options for this good basic query version...
Select 1: Search for special default values
By editing the where Condition Statement, we can view the special default values in all tables.

Quote:
Select so. Name as "table name", SC. Name as "column name", Sm. Text as "Default Value"
From DBO. sysobjects so inner join DBO. syscolumns SC on so. ID = SC. ID
Left join DBO. syscomments SM on SC. cdefault = Sm. ID
Where so. xtype = 'U' AND Sm. Text = '(0 )'
Order by so. [name], SC. colid
Select 2: only information with default fields is returned.
Modify the where Condition Statement of the core query to ignore the null value in the syscomments. Text table. This technique is as follows:

Quote: Copy code The Code is as follows: Select so. Name as "table name", SC. Name as "column name", Sm. Text as "Default Value"
From DBO. sysobjects so inner join DBO. syscolumns SC on so. ID = SC. ID
Left join DBO. syscomments SM on SC. cdefault = Sm. ID
Where so. xtype = 'U' AND Sm. text is not null
Order by so. [name], SC. colid

however, changing join from a left join to an inner join in the from Condition Clause provides optimization:
quote: copy Code the code is as follows: Select so. name as "table name", SC. name as "column name", SM. text as "Default Value"
from DBO. sysobjects so inner join DBO. syscolumns SC on so. id = SC. ID
Inner join DBO. syscomments SM on SC. cdefault = Sm. ID
where so. xtype = 'U'
order by so. [name], SC. colid


Another option is to use the system directory view in SQL 2005. The previous query provided me with the required information, which can be used in SQL 2000 and SQL 2005, you can mine additional metadata associated with this default value (actually a default constraint) in the SQL2000 instance. By specifying this query in the system directory view, we can obtain additional information not displayed in the previous query.

Quote:Copy codeThe Code is as follows: Select St. [name] as "table name", SC. [name] as "column name", SD. definition as "Default Value", SD. [name] as "constraint name"
From sys. Tables st inner join SYS. syscolumns SC on st. [object_id] = SC. [ID]
Inner join SYS. default_constraints SD on st. [object_id] = SD. [parent_object_id] and SC. colid = SD. parent_column_id
Order by St. [name], SC. colid


So remember, because you are told that there is no better way to study it by your instinct as a database administrator. You will never know what you may get.

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.