¿Cómo se cambia el tipo de datos de una columna en SQL Server?
Estoy tratando de cambiar una columna de un varchar(50)
a un nvarchar(200)
. ¿Qué es el comando SQL para alterar esta tabla?
7 answers
ALTER TABLE TableName
ALTER COLUMN ColumnName NVARCHAR(200) [NULL | NOT NULL]
EDITAR Como se señaló NULL / NOT NULL debería haber sido especificado, ver respuesta de Rob también.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-07-10 12:43:08
No olvides la nullabilidad.
ALTER TABLE <schemaName>.<tableName>
ALTER COLUMN <columnName> nvarchar(200) [NULL|NOT NULL]
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-03-11 16:38:07
Utilice la instrucción Alter table.
Alter table TableName Alter Column ColumnName nvarchar(100)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-02-26 18:08:14
La sintaxis para modificar una columna en una tabla existente en SQL Server (Transact-SQL) es:
ALTER TABLE table_name
ALTER COLUMN column_name column_type;
Por ejemplo:
ALTER TABLE employees
ALTER COLUMN last_name VARCHAR(75) NOT NULL;
Este ejemplo de SQL Server ALTER TABLE
modificará la columna llamada last_name
para que sea un tipo de datos de VARCHAR(75)
y obligará a la columna a no permitir valores nulos.
Ver aquí
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-10-12 06:39:18
Mientras estés aumentando el tamaño de tu varchar estás bien. Según la referencia Alter Table :
Reducir la precisión o escala de una columna puede causar truncamiento de datos.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-12-23 18:25:05
Para cambiar el tipo de datos
alter table table_name
alter column column_name datatype [NULL|NOT NULL]
Para cambiar la clave Primaria
ALTER TABLE table_name
ADD CONSTRAINT PK_MyTable PRIMARY KEY (column_name)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-09-26 20:12:49
Prueba esto:
ALTER TABLE "table_name"
MODIFY "column_name" "New Data Type";
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-01-29 14:10:25