Alter Column (T-SQL)

Hi. I'm learning T-SQL programming. I've a problem, just how to "Alter" 2 columns? I try many variants but cant solve it.

  1. Alter Table NewBase
     Alter Column Name Int, Alter Column Surname Int
    
  2. Alter Table NewBase
     Alter Column Name Int, Column Surname Int
    
  3. Alter Table NewBase
     Alter Column Name Int, Alter Surname Int
    
  4. Alter Table NewBase
     Alter Column Name Int
     Alter Column Surname Int
    
  5. Alter Table NewBase
     Alter Column Name Int
             Column Surname Int ... 
    

Thansk a lot

hi

it should be .. a seperate statement for each column

Alter Table NewBase Alter Column Name Int
Alter Table NewBase Alter Column Surname Int

1 Like
ALTER TABLE NewBase
ALTER COLUMN Name  Int;

ALTER TABLE NewBase
ALTER COLUMN Surname  Int

Of course this will fail to work if the table columns you are altering are already prepopulated with string values

1 Like

You must always specify NULLability when altering a column. Otherwise, you could accidentally get the wrong NULL setting.

ALTER TABLE NewBase ALTER COLUMN Name Int NOT NULL; /* or NULL, whichever you need */

SQL always (re)sets the NULLability when you ALTER a column, although it does not issue any message about it. The default for NULL / NOT NULL is very complex to figure out, so it's better to just explicitly state it every time. Btw, the same thing applies when creating tables, never assume that NULL or NOT NULL will be the default, always explicitly state it.

1 Like