日期:2014-05-18 浏览次数:20539 次
create table company ( id int identity primary key, names nvarchar(20) ) insert into company(names) values('中部印刷网') insert into company(names) values('睿智软件') go create table job ( id int identity primary key, jobName nvarchar(20), companyName nvarchar(20), companyId int ) go insert into job(jobName,companyName,companyId) values('程序员','中部印刷网',null) insert into job(jobName,companyName,companyId) values('美工','中部印刷网',null) insert into job(jobName,companyName,companyId) values('软件工程师','睿智软件',null) go --1 update job set companyId = (select id from company where names = t.companyName) from job t --2 update job set companyId = m.id from job t , company m where t.companyName = m.names select * from job /* id jobName companyName companyId ----------- -------------------- -------------------- ----------- 1 程序员 中部印刷网 1 2 美工 中部印刷网 1 3 软件工程师 睿智软件 2 (所影响的行数为 3 行) */ drop table company , job
------解决方案--------------------
alter table job add company_id varchar(10) default null update b set b.company_id=a.id from job b,company a where b.companyName=a.names