-
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.
-
UPDATE [Users]
SET id_Localisation = loc.id_Localisation
FROM [Users] us, Localisation loc
WHERE loc.Localisation = us.Localisation
--HTH--
-
it works perfectly
thanks a lot
do you thing i have to add a contrainst to this new column ?
-
Should add fkey if you want to keep data integrity.
-
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.
-
Then you may need trigger.
-
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|