1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
-- Recherche de toutes les colonnes de la base courante qui ont le type BIT
DECLARE curBITColumns CURSOR FOR
SELECT OBJECT_NAME(C.object_id) Tables, C.name Colonnes
FROM sys.columns C
JOIN sys.types T ON T.system_type_id = C.system_type_id
WHERE T.NAME = 'bit'
FOR READ ONLY;
DECLARE @table SYSNAME,
@colonne SYSNAME;
DECLARE @SQL VARCHAR(256);
-- Pour chaque colonne trouvée, on met à jour la table, on met la colonne non nullable, puis on ajoute une contrainte de valeur par défaut
OPEN curBITColumns;
FETCH NEXT FROM curBITColumns INTO @table, @colonne;
WHILE @@FETCH_STATUS = 0
BEGIN
SET @SQL = 'UPDATE ' + @table + ' SET ' + @colonne '= 0 WHERE ' + @colonne + ' IS NULL; ALTER TABLE ' + @table +
'ALTER COLUMN ' + @colonne + 'BIT NOT NULL CONSTRAINT DF_' + @table + '_' + @colonne + 'DEFAULT (0)'
PRINT @SQL;
-- EXEC (@SQL);
FETCH NEXT FROM curBITColumns INTO @table, @colonne;
END; |
Partager