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
Oui, bien.J'ai fait un script similaire qui transforme les varchar en nvarchar.Il faut faire attention a ne pas modifier les tables system de la base de données et aussi (dans le cas des datetime c'est rares) de bien supprimer les primary et les foreign key !!ci joint mon code pour les varchar si ca peut aider !!declare sel_table CURSOR FOR select TABLE_NAME = convert(sysname,o.name) from sysobjects o where charindex(substring(o.type,1,1),'U') <> 0declare@table varchar(255),@nomcol varchar(255),@length varchar(6)OPEN sel_tableFETCH NEXT FROM sel_table into @tableWHILE @@FETCH_STATUS = 0 BEGIN declare sel_col cursor for SELECT name,length from syscolumns where id=object_id(@table) and xusertype=(select ss_dtype from master.dbo.spt_datatype_info where upper(TYPE_NAME)='VARCHAR') open sel_col fetch next from sel_col into @nomcol,@length while @@FETCH_STATUS =0 begin print 'alter table '+@table+' alter column '+@nomcol+' nvarchar('+@length+')' print 'GO' fetch next from sel_col into @nomcol,@length end close sel_col DEALLOCATE sel_col FETCH NEXT FROM sel_table into @table ENDCLOSE sel_tableDEALLOCATE sel_table
autant pour moi, le scripts précédents génère un script garce au print!voici le script a exécuter directement :/* ATTENTION , ce script passe tous les champs de type varchar de la base de données courrante en nvarchar de la meme longueur *//* il faut supprimer tous les objets lié a ces varchar (primary Key, constraint, index ...) */declare sel_table CURSOR FOR select TABLE_NAME = convert(sysname,o.name) from sysobjects o where charindex(substring(o.type,1,1),'U') <> 0declare@table varchar(255),@nomcol varchar(255),@length varchar(6)OPEN sel_tableFETCH NEXT FROM sel_table into @tableWHILE @@FETCH_STATUS = 0 BEGIN declare sel_col cursor for SELECT name,length from syscolumns where id=object_id(@table) and xusertype=(select ss_dtype from master.dbo.spt_datatype_info where upper(TYPE_NAME)='VARCHAR') open sel_col fetch next from sel_col into @nomcol,@length while @@FETCH_STATUS =0 begin execute('alter table '+@table+' alter column '+@nomcol+' nvarchar('+@length+')') fetch next from sel_col into @nomcol,@length end close sel_col DEALLOCATE sel_col FETCH NEXT FROM sel_table into @table ENDCLOSE sel_tableDEALLOCATE sel_table
Se souvenir du profil
Mot de passe oublié ? / Activation de compteCréer un compte
1 872 826 membres 255 nouveaux aujourd'hui 16 151 membres club