sp_helpconstraint (Transact-SQL)
Applies to: SQL Server Azure SQL Managed Instance
Returns a list of all constraint types, their user-defined or system-supplied name, the columns on which they're defined, and the expression that defines the constraint (for DEFAULT
and CHECK
constraints only).
Transact-SQL syntax conventions
Syntax
sp_helpconstraint
[ @objname = ] N'objname'
[ , [ @nomsg = ] 'nomsg' ]
[ ; ]
Arguments
[ @objname = ] N'objname'
Specifies the table for which the constraint information is returned. @objname is nvarchar(776), with no default. The table specified must be local to the current database.
[ @nomsg = ] 'nomsg'
An optional parameter that prints the table name. @nomsg is varchar(5), with a default of msg
. nomsg
suppresses the printing.
Return code values
0
(success) or 1
(failure).
Result set
sp_helpconstraint
displays a descending indexed column if it participated in primary keys. The descending indexed column is listed in the result set with a minus sign (-
) following its name. The default, an ascending indexed column, is listed by its name alone.
Remarks
Executing sp_help <table>
reports all information about the specified table. To see only the constraint information, use sp_helpconstraint
.
Permissions
Requires membership in the public role.
Examples
The Transact-SQL code samples in this article use the AdventureWorks2022
or AdventureWorksDW2022
sample database, which you can download from the Microsoft SQL Server Samples and Community Projects home page.
The following example shows all constraints for the Product.Product
table.
USE AdventureWorks2022;
GO
EXEC sp_helpconstraint 'Production.Product';