Alter Table Add Column Syntax


Estoy tratando de agregar programáticamente una columna de identidad a una tabla Empleados. No estoy seguro de lo que estoy haciendo mal con mi sintaxis.

ALTER TABLE Employees
  ADD COLUMN EmployeeID int NOT NULL IDENTITY (1, 1)

ALTER TABLE Employees ADD CONSTRAINT
    PK_Employees PRIMARY KEY CLUSTERED 
    (
      EmployeeID
    ) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, 
    ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

¿Qué estoy haciendo mal? Traté de exportar el script, pero SQL Mgmt Studio hace toda una tabla Temporal cambiar el nombre de la cosa.

ACTUALIZACIÓN : Creo que se está ahogando en la primera declaración con "Sintaxis incorrecta cerca de la palabra clave 'COLUMNA'."

Author: GEOCHET, 2009-04-27

4 answers

Simplemente elimina COLUMN de ADD COLUMN

ALTER TABLE Employees
  ADD EmployeeID numeric NOT NULL IDENTITY (1, 1)

ALTER TABLE Employees ADD CONSTRAINT
        PK_Employees PRIMARY KEY CLUSTERED 
        (
          EmployeeID
        ) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, 
        ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
 168
Author: Vikram,
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-04-27 17:02:48

Así es como se Agrega una nueva columna a la Tabla

ALTER TABLE [tableName]
ADD ColumnName Datatype

Por ejemplo

ALTER TABLE [Emp]
ADD Sr_No Int

Y si quieres que se incremente automáticamente

ALTER TABLE [Emp]
ADD Sr_No Int IDENTITY(1,1) NOT NULL
 6
Author: Chirag Thakar,
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-04-10 11:15:27

La sintaxis correcta para agregar columnas a la tabla es:

ALTER TABLE table_name
  ADD column_name column-definition;

En su caso será:

ALTER TABLE Employees
  ADD EmployeeID int NOT NULL IDENTITY (1, 1)

Para agregar varias columnas use corchetes:

ALTER TABLE table_name
  ADD (column_1 column-definition,
       column_2 column-definition,
       ...
       column_n column_definition);

COLUMN la palabra clave en SQL SERVER se usa solo para alterar:

ALTER TABLE table_name
  ALTER COLUMN column_name column_type;
 3
Author: Dzianis Yafimau,
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-01-27 08:58:09

Podría estar haciendo el cambio de nombre de la tabla temporal si está tratando de agregar una columna al principio de la tabla (ya que esto es más fácil que alterar el orden). Además, si hay datos en la tabla Empleados, tiene que hacer insert select * para que pueda calcular el EmployeeID.

 0
Author: neouser99,
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-04-27 17:03:53