Results 1 to 7 of 7

Thread: Alter table new column and update

  1. #1
    Join Date
    Apr 2006
    Posts
    178

    Alter table new column and update

    Hi

    for MS SQL 2000/2005

    I am having a table (an old database, not mine) with char value for the column [localisation]

    Users
    [name] [nvarchar] (100) NOT NULL ,
    [localisation] [nvarchar] (100)NULL


    Now i have created a table [Localisation]

    Localisation
    [id_Localisation] [int] NOT NULL,
    [localisation] [nvarchar] (100) NOT NULL


    I am adding a new column to Users

    ALTER TABLE [Users] ADD
    [id_Localisation] int NULL


    and I want to update the Column [Users].[id_Localisation] before to drop the column [Users].[Localisation]

    something like

    UPDATE [Users] SET id_Localisation = (SELECT Localisation.id_Localisation
    FROM Localisation FULL OUTER JOIN
    Users ON Localisation.Localisation = Users.Localisation)


    Users.Localisation can have a NULL value (then no id_localisation returned)

    but it doesnt work because it returns > 1 row

    thank you

    how can I do it ?
    Last edited by anselme; 11-17-2006 at 01:48 AM.

  2. #2
    Join Date
    Sep 2005
    Posts
    168
    UPDATE [Users]
    SET id_Localisation = loc.id_Localisation
    FROM [Users] us, Localisation loc
    WHERE loc.Localisation = us.Localisation

    --HTH--

  3. #3
    Join Date
    Apr 2006
    Posts
    178
    it works perfectly

    thanks a lot

    do you thing i have to add a contrainst to this new column ?

  4. #4
    Join Date
    Sep 2002
    Posts
    5,938
    Should add fkey if you want to keep data integrity.

  5. #5
    Join Date
    Apr 2006
    Posts
    178
    but 5 tables are using this id_Localisation, can i add a FK to each one ?
    FK_FK_Users_Localisation
    FK_job_Localisation
    FK_groups_Localisation
    ......

    if so

    5 times (for each tables)

    ALTER TABLE [Users] ADD
    id_Localisation int NULL

    ALTER TABLE [Users] WITH NOCHECK ADD
    CONSTRAINT [FK_Users_Localisation] FOREIGN KEY
    (
    [id_Localisation]
    ) REFERENCES [Localisation] (
    [id_Localisation]
    )


    I dont want to apply ON DELETE CASCADE , but to give a Id_localisation = 0 or NULL if a Localisation is deleted, how can i do it
    ??

    thanks again for helping
    Last edited by anselme; 11-19-2006 at 04:34 AM.

  6. #6
    Join Date
    Sep 2002
    Posts
    5,938
    Then you may need trigger.

  7. #7
    Join Date
    Apr 2006
    Posts
    178
    ok !
    thank you

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •