Professional Documents
Culture Documents
Check constraints creation statements were also created previously using the following TSQL:
1
4 SELECT
5 schemas.name AS Schema_Name,
6 tables.name AS Table_Name,
8 WITH '' + CASE WHEN check_constraints.is_not_trusted = 1 THEN ''NOCHECK'' ELSE ''CHECK'' END + '' ADD
FROM sys.check_constraints
11
ON tables.object_id = check_constraints.parent_object_id
13
ON tables.schema_id = schemas.schema_id';
15
BEGIN
17
END
20
BEGIN
22 SELECT @Sql_Command = @Sql_Command + '
24 END
26 BEGIN
29 END
30
34
While no column lists were needed for this work, we did need to verify if a constraint was created
with the NOCHOCK attribute. Otherwise, the creation statement is relatively simple. Optional
filters on schema and table help reduce the data returned to include only what we are interested
in based on the stored proc parameters. Once we have the creation script, we can script out the
check constraints as follows:
1
Table_Name = @Table_Name_Current)
3
BEGIN
4
SELECT
5
7
9 FROM @Check_Constraints
12 END
13
If any check constraints exist, then add the previously-created scripts to our growing table
creation script.