Skip to main content
Sorry, one more change, in case people name their table `User` or `Table`.
Source Link
Aaron Bertrand
  • 282.4k
  • 37
  • 471
  • 469

If you want to drop all the constraints from your first query, you can build up a dynamic SQL with the results of that query and then execute it:

DECLARE @sql AS NVARCHAR(MAX) = N''

SELECT @sql = @sql + N'ALTER TABLE ' 
      + QUOTENAME(schemas.name) + N'.' + QUOTENAME(tables.name) 
      + N' DROP CONSTRAINT ' + QUOTENAME(default_constraints.name) + N'; '
FROM 
    sys.all_columns
        INNER JOIN
    sys.tables
        ON all_columns.object_id = tables.object_id
        INNER JOIN 
    sys.schemas
        ON tables.schema_id = schemas.schema_id
        INNER JOIN
    sys.default_constraints
        ON all_columns.default_object_id = default_constraints.object_id
WHERE 
        schemas.name = N'dbo'
    and default_constraints.definition like N'%1899%'
    and all_columns.is_nullable = 1
order by tables.name, all_columns.name, default_constraints.name

exec (@sql)sp_executesql @sql;

If you want to drop all the constraints from your first query, you can build up a dynamic SQL with the results of that query and then execute it:

DECLARE @sql AS NVARCHAR(MAX) = N''

SELECT @sql = @sql + N'ALTER TABLE ' + schemas.name + N'.' + tables.name + N' DROP CONSTRAINT ' + default_constraints.name + N'; '
FROM 
    sys.all_columns
        INNER JOIN
    sys.tables
        ON all_columns.object_id = tables.object_id
        INNER JOIN 
    sys.schemas
        ON tables.schema_id = schemas.schema_id
        INNER JOIN
    sys.default_constraints
        ON all_columns.default_object_id = default_constraints.object_id
WHERE 
        schemas.name = N'dbo'
    and default_constraints.definition like N'%1899%'
    and all_columns.is_nullable = 1
order by tables.name, all_columns.name, default_constraints.name

exec (@sql)

If you want to drop all the constraints from your first query, you can build up a dynamic SQL with the results of that query and then execute it:

DECLARE @sql AS NVARCHAR(MAX) = N''

SELECT @sql = @sql + N'ALTER TABLE ' 
      + QUOTENAME(schemas.name) + N'.' + QUOTENAME(tables.name) 
      + N' DROP CONSTRAINT ' + QUOTENAME(default_constraints.name) + N'; '
FROM 
    sys.all_columns
        INNER JOIN
    sys.tables
        ON all_columns.object_id = tables.object_id
        INNER JOIN 
    sys.schemas
        ON tables.schema_id = schemas.schema_id
        INNER JOIN
    sys.default_constraints
        ON all_columns.default_object_id = default_constraints.object_id
WHERE 
        schemas.name = N'dbo'
    and default_constraints.definition like N'%1899%'
    and all_columns.is_nullable = 1
order by tables.name, all_columns.name, default_constraints.name

exec sp_executesql @sql;
added 29 characters in body
Source Link
Szymon
  • 43k
  • 16
  • 100
  • 115

If you want to drop all the constraints from your first query, you can build up a dynamic SQL with the results of that query and then execute it:

DECLARE @sql AS VARCHARNVARCHAR(MAX) = ''N''

SELECT @sql = @sql + 'ALTERN'ALTER TABLE ' + tablesschemas.name + N'.' + tables.name + N' DROP CONSTRAINT ' + default_constraints.name + ';N'; '
FROM 
    sys.all_columns
        INNER JOIN
    sys.tables
        ON all_columns.object_id = tables.object_id
        INNER JOIN 
    sys.schemas
        ON tables.schema_id = schemas.schema_id
        INNER JOIN
    sys.default_constraints
        ON all_columns.default_object_id = default_constraints.object_id
WHERE 
        schemas.name = 'dbo'N'dbo'
    and default_constraints.definition like '%1899%'N'%1899%'
    and all_columns.is_nullable = 1
order by tables.name, all_columns.name, default_constraints.name

exec (@sql)

If you want to drop all the constraints from your first query, you can build up a dynamic SQL with the results of that query and then execute it:

DECLARE @sql AS VARCHAR(MAX) = ''

SELECT @sql = @sql + 'ALTER TABLE ' + tables.name + ' DROP CONSTRAINT ' + default_constraints.name + '; '
FROM 
    sys.all_columns
        INNER JOIN
    sys.tables
        ON all_columns.object_id = tables.object_id
        INNER JOIN 
    sys.schemas
        ON tables.schema_id = schemas.schema_id
        INNER JOIN
    sys.default_constraints
        ON all_columns.default_object_id = default_constraints.object_id
WHERE 
        schemas.name = 'dbo'
    and default_constraints.definition like '%1899%'
    and all_columns.is_nullable = 1
order by tables.name, all_columns.name, default_constraints.name

exec (@sql)

If you want to drop all the constraints from your first query, you can build up a dynamic SQL with the results of that query and then execute it:

DECLARE @sql AS NVARCHAR(MAX) = N''

SELECT @sql = @sql + N'ALTER TABLE ' + schemas.name + N'.' + tables.name + N' DROP CONSTRAINT ' + default_constraints.name + N'; '
FROM 
    sys.all_columns
        INNER JOIN
    sys.tables
        ON all_columns.object_id = tables.object_id
        INNER JOIN 
    sys.schemas
        ON tables.schema_id = schemas.schema_id
        INNER JOIN
    sys.default_constraints
        ON all_columns.default_object_id = default_constraints.object_id
WHERE 
        schemas.name = N'dbo'
    and default_constraints.definition like N'%1899%'
    and all_columns.is_nullable = 1
order by tables.name, all_columns.name, default_constraints.name

exec (@sql)
Source Link
Szymon
  • 43k
  • 16
  • 100
  • 115

If you want to drop all the constraints from your first query, you can build up a dynamic SQL with the results of that query and then execute it:

DECLARE @sql AS VARCHAR(MAX) = ''

SELECT @sql = @sql + 'ALTER TABLE ' + tables.name + ' DROP CONSTRAINT ' + default_constraints.name + '; '
FROM 
    sys.all_columns
        INNER JOIN
    sys.tables
        ON all_columns.object_id = tables.object_id
        INNER JOIN 
    sys.schemas
        ON tables.schema_id = schemas.schema_id
        INNER JOIN
    sys.default_constraints
        ON all_columns.default_object_id = default_constraints.object_id
WHERE 
        schemas.name = 'dbo'
    and default_constraints.definition like '%1899%'
    and all_columns.is_nullable = 1
order by tables.name, all_columns.name, default_constraints.name

exec (@sql)