问一个update的sql
-- data
id id_emp emp_make
1 1 小明
2 2 小明
3 2 中明
4 大明
5 无明
-- employee
id emp
1 小明
2 中明
3 大明
id id_emp emp_make
1 1 小明
2 2 小明
3 2 中明
4 3 大明
5 无明
-- ----------------------------
-- Table structure for [dbo].[employee]
-- ----------------------------
DROP TABLE [dbo].[employee]
GO
CREATE TABLE [dbo].[employee] (
[id] int NOT NULL ,
[emp] nvarchar(10) NULL
)
GO
-- ----------------------------
-- Records of employee
-- ----------------------------
INSERT INTO [dbo].[employee] ([id], [emp]) VALUES (N'1', N'小明');
GO
INSERT INTO [dbo].[employee] ([id], [emp]) VALUES (N'2', N'中明');
GO
INSERT INTO [dbo].[employee] ([id], [emp]) VALUES (N'3', N'大明');
GO
-- ----------------------------
-- Indexes structure for table employee
-- ----------------------------
-- ----------------------------
-- Primary Key structure for table [dbo].[employee]
-- ----------------------------
ALTER TABLE [dbo].[employee] ADD PRIMARY KEY ([id])
GO
-- ----------------------------
-- Table structure for [dbo].[data]
-- ----------------------------
DROP TABLE [dbo].[data]
GO
CREATE TABLE [dbo].[data] (
[id] int NOT NULL ,
[id_emp] int NULL ,
[emp_make] nvarchar(10) NULL
)
GO
-- ----------------------------
-- Records of data
-- ----------------------------
INSERT INTO [dbo].[data] ([id], [id_emp], [emp_make]) VALUES (N'1', N'1', N'小明');
GO
INSERT INTO [dbo].[data] ([id], [id_emp], [emp_make]) VALUES (N'2', N'2', N'小明');
GO
INSERT INTO [dbo].[data] ([id], [id_emp], [emp_make]) VALUES (N'3', N'2', N'小明');
GO
INSERT INTO [dbo].[data] ([id], [id_emp], [emp_make]) VALUES (N'4', null, N'大明');
GO
INSERT INTO [dbo].[data] ([id], [id_emp], [emp_make]) VALUES (N'5', null, N'无明');
GO
-- ----------------------------
-- Indexes structure for table data
-- ----------------------------
-- ----------------------------
-- Primary Key structure for table [dbo].[data]
-- ----------------------------
ALTER TABLE [dbo].[data] ADD PRIMARY KEY ([id])
GO
-- ----------------------------
-- Foreign Key structure for table [dbo].[data]
-- ----------------------------
ALTER TABLE [dbo].[data] ADD FOREIGN KEY ([id_emp]) REFERENCES [dbo].[employee] ([id]) ON DELETE NO ACTION ON UPDATE NO ACTION
GO
[解决办法]
UPDATE [data]
SET id_emp=employee.id
FROM [data] LEFT JOIN employee ON [data].emp_make=employee.emp
WHERE ISNULL([data].id_emp,'')=''
[解决办法]
update [data]
set [id_emp]=b.[id]
from [data] a,[employee] b
where (a.id_emp is null or a.id_emp='') and a.emp_make=b.emp
update [data]
set [id_emp]=b.[id]
from [employee] b
where ([data].id_emp is null or [data].id_emp='')
and [data].emp_make=b.emp
update a
set a.id_emp=b.id
from [data] a
inner join [employee] b on b.emp=a.emp_make
where a.id_emp is null or a.id_emp=''
select * from [data]
/*
id id_emp emp_make
----------- ----------- ----------
1 1 小明
2 2 小明
3 2 中明
4 3 大明
5 NULL 无明
(5 row(s) affected)
*/
update [data]
set [id_emp]=b.[id]
from [employee] b
where ([data].id_emp is null or [data].id_emp='')
and [data].emp_make=b.emp
select *
from data
/*
idid_empemp_make
11小明
22小明
32小明
43大明
5NULL无明
*/