Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Alter table alter column Syntax
Message
De
18/03/2024 05:34:32
 
 
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Conception bases de données
Divers
Thread ID:
01687733
Message ID:
01687746
Vues:
40
>>>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.
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform