Created
July 24, 2024 12:38
-
-
Save ststeiger/b0d5afb5f8dfd3f9058f2c08dfbc2ecf to your computer and use it in GitHub Desktop.
Fix an MS-SQL default-value for table-column
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DECLARE @schemaName nvarchar(128); | |
DECLARE @tableName nvarchar(128); | |
DECLARE @columnName nvarchar(128); | |
DECLARE @constraintName nvarchar(128); | |
DECLARE @dropConstraintSQL nvarchar(MAX); | |
SET @schemaName = 'dbo'; | |
SET @tableName = 'T_ChecklistVersion'; | |
SET @columnName = 'CLV_Obsoleted'; | |
SET @constraintName = | |
( | |
SELECT TOP 1 | |
-- sch.name, t.name, | |
dc.name AS default_constraint_name | |
FROM sys.default_constraints AS dc | |
INNER JOIN sys.columns AS c | |
ON dc.parent_column_id = c.column_id | |
AND dc.parent_object_id = c.object_id | |
INNER JOIN sys.tables AS t | |
ON t.object_id = c.object_id | |
INNER JOIN sys.schemas AS sch | |
ON sch.schema_id = t.schema_id | |
WHERE (1=1) | |
AND sch.name = @schemaName | |
AND t.name = @tableName | |
AND c.name = @columnName | |
); | |
SET @dropConstraintSQL = N'ALTER TABLE ' | |
+ QUOTENAME(@schemaName) + N'.' + QUOTENAME(@tableName) | |
+ N' DROP CONSTRAINT ' + QUOTENAME(@constraintName) + N'; ' | |
; | |
-- DF_T_ChecklistVersion_CLV_Obsoleted | |
-- SELECT @dropConstraintSQL | |
EXECUTE(@dropConstraintSQL); | |
ALTER TABLE dbo.T_ChecklistVersion | |
ADD CONSTRAINT DF_T_ChecklistVersion_CLV_Obsoleted | |
DEFAULT '29991231' FOR CLV_Obsoleted; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment