Groups | Blog | Home
all groups > sql server programming > october 2003 >

sql server programming : help with update statement


Carla
10/20/2003 10:47:49 PM
create table parent_tb (
department_id int identity (1,1) primary key,
department_nm varchar(25))

insert into parent_tb
(department_nm) values('finance')
insert into parent_tb
(department_nm) values ('marketing')
insert into parent_tb
(department_nm) values ('sales')
insert into parent_tb
(department_nm) values ('hr')

create table child_tb (
employee_id int identity (1,1) primary key,
department_id int
references parent_tb(department_id),
employee_nm varchar(25))

insert into child_tb
(department_id, employee_nm) values (1, 'Bob')
insert into child_tb
(department_id, employee_nm) values (2, 'Tim')
insert into child_tb
(department_id, employee_nm) values (3, 'Tom')
insert into child_tb
(department_id, employee_nm) values (3, 'Tom')

-- now I need to update the department_id with
department_name. Something like this

update child_tb
set department_id=parent_tb.department_nm
from parent_tb, child_tb
where parent_tb.department_id=child_tb.department_id

-- But this way I get a cartisian product. I'm not sure
how I can do this update.

oj
10/20/2003 11:01:35 PM
Carla,

You cannot update child.department_id with parent.department_nm. First of
all, you cannot update an Int column with a varchar column. Second, you're
violating the referential integrity.

What are you really trying to do.

--
-oj
RAC v2.2 & QALite!
http://www.rac4sql.net



[quoted text, click to view]

Carla
10/21/2003 9:08:53 AM
I know it sounds absurd but that is what I need to do.

Actually, you are right about the datatype fields and
referencial constraint violation but I'm creating a third
table to store the child table after the updates. This
third table (lets call child_tb_extract) does not have the
referencial constraint and the department_id is a varchar
field. (It's actually a .txt file but I'm dumping into a
table just to check the results)

The reason why I need to do this is because I need to
translate the artificial keys to natural keys. In my
example the artificial key would be the department_id and
the department_nm would be the natural key.

In my real assigment the artificial key is an int and the
natural key is varchar. Although both are represented by a
number. So, it match exactly the example I gave.

Again, any help is very much appreciated.

Carla

[quoted text, click to view]
AddThis Social Bookmark Button