Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Alter table alter column Syntax
Message
General information
Forum:
Microsoft SQL Server
Category:
Database design
Miscellaneous
Thread ID:
01687733
Message ID:
01687746
Views:
39
>>>The column had the DEFAULT set before I made the change of the column size. And I do prefer not to change the default. Do I understand correctly that the DEFAULT will stay the way it was before I change the size?
>>>Thank you.
>>
>>In earlier versions of SQL Server I always got "Can not ALTER ... there is a constraint' something like this (can't remember the exact text).
>>That is why I have this habit to remove default constraint, change the size and put the constraint back.
>>Maybe MS changed this behavior lately and there is no more error, I don't know.
>
>Thank you for clarifying this point. Say, I have to remove the Default constraint before setting the new column size, how do I find out if the Column has a Default Constraint? And how would you remove it?
DECLARE @colname nvarchar(200)
DECLARE @sql  nvarchar(200)
SELECT @colname = MIN(sysobjcnstr.NAME)
        FROM sys.objects sysobj
INNER JOIN sys.objects sysobjcnstr        ON sysobj.object_id    = sysobjcnstr.parent_object_id 
INNER JOIN sys.sysconstraints constraints ON constraints.constid = sysobjcnstr.object_id 
INNER JOIN sys.columns clmns              ON clmns.object_id     = sysobj.object_id 
                                        AND clmns.column_id     = constraints.colid 
WHERE sysobj.NAME   ='YourTableName'
    AND clmns.NAME  = 'YourColumnName'


WHILE @colname IS NOT NULL
    BEGIN
        SET @sql = 'ALTER TABLE YourTableName DROP CONSTRAINT '+@colname
        print @sql
        --EXEC (@sql)

        SELECT @colname = MIN(sysobjcnstr.NAME)
               FROM sys.objects sysobj
        INNER JOIN sys.objects sysobjcnstr        ON sysobj.object_id    = sysobjcnstr.parent_object_id 
        INNER JOIN sys.sysconstraints constraints ON constraints.constid = sysobjcnstr.object_id 
        INNER JOIN sys.columns clmns              ON clmns.object_id     = sysobj.object_id 
                                                 AND clmns.column_id     = constraints.colid 
        WHERE sysobj.NAME='YourTableName'
          AND clmns.NAME = 'YourColumnName'
          AND sysobjcnstr.NAME > @colname
    END
Against Stupidity the Gods themselves Contend in Vain - Johann Christoph Friedrich von Schiller
The only thing normal about database guys is their tables.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform