CHANGER TOUTES LES COLONNES D'UNE BASES DE DONNÉES D'UN TYPE VERS UN AUTRE [SQL

cs_Escob Messages postés 14 Date d'inscription vendredi 4 avril 2003 Statut Membre Dernière intervention 14 juin 2008 - 28 mai 2007 à 14:57
cs_Escob Messages postés 14 Date d'inscription vendredi 4 avril 2003 Statut Membre Dernière intervention 14 juin 2008 - 28 mai 2007 à 15:00
Cette discussion concerne un article du site. Pour la consulter dans son contexte d'origine, cliquez sur le lien ci-dessous.

https://codes-sources.commentcamarche.net/source/42839-changer-toutes-les-colonnes-d-une-bases-de-donnees-d-un-type-vers-un-autre-sql-2000

cs_Escob Messages postés 14 Date d'inscription vendredi 4 avril 2003 Statut Membre Dernière intervention 14 juin 2008
28 mai 2007 à 15:00
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') <> 0

declare
@table varchar(255),
@nomcol varchar(255),
@length varchar(6)

OPEN sel_table
FETCH NEXT FROM sel_table into @table
WHILE @@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
END
CLOSE sel_table
DEALLOCATE sel_table
cs_Escob Messages postés 14 Date d'inscription vendredi 4 avril 2003 Statut Membre Dernière intervention 14 juin 2008
28 mai 2007 à 14:57
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') <> 0

declare
@table varchar(255),
@nomcol varchar(255),
@length varchar(6)

OPEN sel_table
FETCH NEXT FROM sel_table into @table
WHILE @@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
END
CLOSE sel_table
DEALLOCATE sel_table
Rejoignez-nous