Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Cambiar de varchar para nvarchar

145 views
Skip to first unread message

Nuno Santos

unread,
Jun 1, 2005, 9:22:33 AM6/1/05
to
 
Holá grupo,
 
    Estoy con un pequeño problema, les explico:
    Tengo una base de datos con unas 300 tabelas, y tengo todos los campos como VARCHAR(), ahora bien me pidieron para alterar el tipo
varchar para NVARCHAR, alguno de vos sabe como lo puedo hacer de una forma rápida. Pués no me estoy a ver a ir tabla a tabla, campo a campo alterar el tipo.
 
 
Gracias de antemano
Nuno Santos
 

--
------------------------------------------------------
Nuno Santos
nunos7[REMOVER]@hotmail.com
------------------------------------------------------

Gustavo Larriera [MVP]

unread,
Jun 1, 2005, 12:14:43 PM6/1/05
to
Un pseudoalgorimo que se me ocurre es:
 
FOREACH (CAMPO VARCHAR EN MIS TABLAS), es decir:
    SELECT table_name, column_name, character_maximum_length
    FROM information_schema.columns
    WHERE data_type = 'varchar'
 
BEGIN
    CONSTRUIR EL STRING SQL DINAMICO "ALTER TABLE <table_name> ALTER COLUMN <column_name> VARCHAR(<character_maximum_length >)"
    EJECUTAR EL SQL DINAMICO
END FOREACH
 
Espero que te sirva de ayuda para atacar el problema.
 

--
Gustavo Larriera
Uruguay LatAm
Blog: http://sqljunkies.com/weblog/gux/
MVP profile: http://aspnet2.com/mvp.ashx?GustavoLarriera
--
Este mensaje se proporciona "COMO ESTA" sin garantias y no otorga ningun derecho / This posting is provided "AS IS" with no warranties, and confers no rights.
--
"Nuno Santos" <nunos7[REMOVE]@hotmail.com> wrote in message news:e0JygyqZ...@TK2MSFTNGP15.phx.gbl...

Nuno Santos

unread,
Jun 1, 2005, 12:58:11 PM6/1/05
to
Gracias gustavo voy a tentarcrear una SP con esto para ver que tal me sale...
 
Nuno Santos

--
------------------------------------------------------
Nuno Santos
nunos7[REMOVER]@hotmail.com
------------------------------------------------------
"Gustavo Larriera [MVP]" <gux@_REMOVETHIS_mvps.org> wrote in message news:eSSSFUsZ...@TK2MSFTNGP09.phx.gbl...

Nuno Santos

unread,
Jun 1, 2005, 1:06:01 PM6/1/05
to
Con el select a la  information_schema.columns vienen también las tabelas "dtproperties", como hago para que me vengan unicamente mis tabelas, y nos las tabelas creadas por el sistema, ni views.
 
Gracias por la ayuda
 
Nuno Santos


--
------------------------------------------------------
Nuno Santos
nunos7[REMOVER]@hotmail.com
------------------------------------------------------
"Gustavo Larriera [MVP]" <gux@_REMOVETHIS_mvps.org> wrote in message news:eSSSFUsZ...@TK2MSFTNGP09.phx.gbl...

Gustavo Larriera [MVP]

unread,
Jun 1, 2005, 1:52:34 PM6/1/05
to
SELECT c.table_name, c.column_name, c.character_maximum_length
FROM information_schema.columns c JOIN information_schema.tables t ON c.table_name = t.table_name
WHERE c.data_type = 'varchar'
AND t.table_type = 'BASE TABLE'

--
Gustavo Larriera
Uruguay LatAm
Blog: http://sqljunkies.com/weblog/gux/
MVP profile: http://aspnet2.com/mvp.ashx?GustavoLarriera
--
Este mensaje se proporciona "COMO ESTA" sin garantias y no otorga ningun derecho / This posting is provided "AS IS" with no warranties, and confers no rights.
--
"Nuno Santos" <nunos7[REMOVE]@hotmail.com> wrote in message news:eKx2YvsZ...@TK2MSFTNGP10.phx.gbl...

Alejandro Mesa

unread,
Jun 1, 2005, 3:15:33 PM6/1/05
to
Gustavo,

La idea es magnifica. Lo que si anticipo es que si la columna es
referenciada desde un indice, restriccion de clave foranea, funcion o vista
con atributo schemabinding, entonces el alter va a dar error.

Ejemplo:

use northwind
go

create table t1 (
c1 varchar(25),
c2 varchar(25),
c3 varchar(25) unique
)
go

create index ix_nc_t1_c1 on t1(c1)
go

create view dbo.vw_v1
with schemabinding
as
select c1, c2 from dbo.t1
go

create table t2 (
c1 varchar(25) references t1(c3)
)
go

alter table t1
alter column c1 nvarchar(25)
go

alter table t1
alter column c2 nvarchar(25)
go

alter table t1
alter column c3 nvarchar(25)
go

drop view dbo.vw_v1
go

drop table t2, t1
go

Resultado:

Server: Msg 5074, Level 16, State 3, Line 2
The object 'vw_v1' is dependent on column 'c1'.
Server: Msg 5074, Level 16, State 1, Line 2
The index 'ix_nc_t1_c1' is dependent on column 'c1'.
Server: Msg 4922, Level 16, State 1, Line 2
ALTER TABLE ALTER COLUMN c1 failed because one or more objects access this
column.
Server: Msg 5074, Level 16, State 3, Line 2
The object 'vw_v1' is dependent on column 'c2'.
Server: Msg 4922, Level 16, State 1, Line 2
ALTER TABLE ALTER COLUMN c2 failed because one or more objects access this
column.
Server: Msg 5074, Level 16, State 8, Line 2
The object 'UQ__t1__16A6A769' is dependent on column 'c3'.
Server: Msg 5074, Level 16, State 1, Line 2
The object 'FK__t2__c1__19831414' is dependent on column 'c3'.
Server: Msg 4922, Level 16, State 1, Line 2
ALTER TABLE ALTER COLUMN c3 failed because one or more objects access this
column.


AMB

Gustavo Larriera [MVP]

unread,
Jun 1, 2005, 3:28:08 PM6/1/05
to
100% de acuerdo.

--
Gustavo Larriera
Uruguay LatAm
Blog: http://sqljunkies.com/weblog/gux/
MVP profile: http://aspnet2.com/mvp.ashx?GustavoLarriera
--
Este mensaje se proporciona "COMO ESTA" sin garantias y no otorga ningun
derecho / This posting is provided "AS IS" with no warranties, and confers
no rights.
--

"Alejandro Mesa" <Alejan...@discussions.microsoft.com> wrote in message
news:51076E02-8194-4285...@microsoft.com...

Don Roque

unread,
Jun 1, 2005, 4:51:44 PM6/1/05
to
hay veces que pertenecer a este grupo de noticias me enorgullece.

son grossos muchachos

Nuno Santos

unread,
Jun 2, 2005, 4:17:22 AM6/2/05
to
Muchas gracias a todos, pero mesmo asin haciendo el Join con el tables
continua a aparecer las tabelas creadas por el sistema, como por ejemplo la
"dtproperties"...

Una vez más Gracias,
Nuo Santos

--
------------------------------------------------------
Nuno Santos
nunos7[REMOVER]@hotmail.com
------------------------------------------------------

"Don Roque" <fer...@gmail.com> wrote in message
news:1117659104.2...@z14g2000cwz.googlegroups.com...

Alejandro Mesa

unread,
Jun 2, 2005, 8:08:02 AM6/2/05
to
Nuno,

Usa la funcion OBJECTPROPERTY y excluye las que tengan la propiedad
"IsMSShipped" igual a 1.

Ejemplo:

select
table_schema,
table_name,
column_name
from
information_schema.columns
where
objectproperty(object_id(quotename(table_schema) + '.' +
quotename(table_name)), 'IsMSShipped') = 0
and data_type = 'varchar'
order by
table_schema,
table_name,
ordinal_position


AMB

Nuno Santos

unread,
Jun 2, 2005, 9:13:21 AM6/2/05
to
Gracias a todos.
Nuno Santos

--
------------------------------------------------------
Nuno Santos
nunos7[REMOVER]@hotmail.com
------------------------------------------------------

"Alejandro Mesa" <Alejan...@discussions.microsoft.com> wrote in message

news:DCA6D027-757B-448C...@microsoft.com...

Ricardo Passians

unread,
Jun 4, 2005, 5:58:51 AM6/4/05
to
Oye, y podrías compartir por qué te pidieron ese cambio.  Me da curiosidad.
 
 
"Nuno Santos" <nunos7[REMOVE]@hotmail.com> wrote in message news:e0JygyqZ...@TK2MSFTNGP15.phx.gbl...
 

Nuno Santos

unread,
Jun 6, 2005, 4:08:01 AM6/6/05
to
Por causa do noso software ser traduzido para Ruso.
 
Nuno Santos

--
------------------------------------------------------
Nuno Santos
nunos7[REMOVER]@hotmail.com
------------------------------------------------------
"Ricardo Passians" <rpaSssP...@hotmail.com> wrote in message news:eU5fPwOa...@TK2MSFTNGP15.phx.gbl...
0 new messages