locked
Create a column and insert data to that column RRS feed

  • Question

  • Hi

    I have a table assume Users and i need to add a column and insert a default value.

    the default value is in the same table(Users)

    assume Users table has ID, Name, Password

    Now i need to insert a column as Confirm Password which should be same as Password value.

    How can i alter the table.

    Can anyone help me

    Thanks,

    Harish Reddy

    Tuesday, November 27, 2012 2:22 PM

Answers

  • Hi Harish, can you just add the new column and run an update statement to set the new column equal to the existing column value?  If you need to add any other constraints, you can add them after the update.  Something like:

    -- create table
    create table t1 (i int, j int)
    
    -- insert data
    insert into t1
    select 1, 2
    union 
    select 2, 27
    union
    select 3, 987
    
    -- add new column
    alter table t1
    add k int NULL 
    
    -- update values in new column
    update t1
    set k = j
    
    

    Thanks,
    Sam Lester (MSFT)


    My Blog

    This posting is provided "AS IS" with no warranties, and confers no rights. Please remember to click "Mark as Answer" and "Vote as Helpful" on posts that help you. This can be beneficial to other community members reading the thread.

    Tuesday, November 27, 2012 2:37 PM