Ce code permet de changer toutes les colonnes d'un certain type vers un autre type.
Ce code est issu d'une source de fabrice69 :
http://www.sqlfr.com/codes/SQL-SERVER-2005-CHANGER-TYPE-DONNEES-TOUTES-COLONNES_40095.aspx
Source / Exemple :
DECLARE @NewType AS VARCHAR(50)
DECLARE @AncType AS VARCHAR(50)
DECLARE @SQLCMD AS VARCHAR(500)
SET @AncType = 'smalldatetime'
SET @NewType = 'datetime'
DECLARE CURSORLISTCHEMA CURSOR FOR SELECT 'ALTER TABLE ['+ TABLE_SCHEMA +'].['+ TABLE_NAME +'] ALTER COLUMN '+ COLUMN_NAME +' '+ @NewType
FROM INFORMATION_SCHEMA.COLUMNS WHERE DATA_TYPE = @AncType
OPEN CURSORLISTCHEMA
FETCH NEXT FROM CURSORLISTCHEMA INTO @SQLCMD
WHILE (@@fetch_status >= 0)
BEGIN
PRINT @SQLCMD
EXECUTE(@SQLCMD)
FETCH NEXT FROM CURSORLISTCHEMA INTO @SQLCMD
END
CLOSE CURSORLISTCHEMA
DEALLOCATE CURSORLISTCHEMA