merge into A using B on B.col1 = A.col1 when Matched then update set B.col1 = A.col1 when not Matched then insert (col1,col2,col3) values (select col1, col2, col3 from A)
merge into A using B on B.col1 = A.col1 when Matched then update set B.col1 = A.col1 when not Matched then insert (col1,col2,col3) values (select col1, col2, col3 from A where col3 >80)
--创建测试表 CREATE TABLE t_A (MCLASS VARCHAR(20),SNO VARCHAR(20)) INSERT INTO t_A SELECT 'L-1','123' UNION ALL SELECT 'L-2','234' UNION ALL SELECT 'L-3','345'
CREATE TABLE t_B (MCLASS VARCHAR(20),SNO VARCHAR(20)) INSERT INTO t_B SELECT 'L-1','999' UNION ALL SELECT 'L-2','999' UNION ALL SELECT 'L-4','888'
--确定目标表 merge into t_A as a --从t_B表中查找MCLASS相同的数据 using t_B as b on a.MCLASS=b.MCLASS and a.mclass= 'L-1'--这里写条件 --当MCLASS相同时,则更新t_A中SNO字段 when Matched then update set a.SNO=b.SNO --当t_A表中不存在MCLASS相同的数据时,则从t_B表插入目标表t_A when Not Matched then Insert(MCLASS,SNO) values(b.MCLASS,b.SNO);
select * from t_A select * from t_B drop table t_A drop table t_B
#12
merge into A using B on B.col1 = A.col1 --这里写条件 and a.col3 >80
CREATE TRIGGER Reminder_Helpdesk on dbo.helpdesk after update as begin declare @INCIDENT_ID nvarchar(15) declare @PRIORITY nvarchar(20) declare @PRIORITY_NEW nvarchar(20)
IF UPDATE (PRIORITY) BEGIN
declare cur_helpdesk cursor for select INCIDENT_ID, PRIORITY from DELETED open cur_helpdesk fetch next from cur_helpdesk into @INCIDENT_ID, @PRIORITY while @@FETCH_STATUS =0 begin select @PRIORITY_new=[PRIORITY] from INSERTED where INCIDENT_ID=INCIDENT_ID if (@PRIORITY_new<@PRIORITY) begin insert into UpdateLog(priority) values(@PRIORITY+' '+@PRIORITY_new) end fetch next from cur_helpdesk into @INCIDENT_ID, @PRIORITY end begin EXEC msdb.dbo.sp_send_dbmail
@recipients = 'test@163.com', @message = 'There is message', @subject = 'There is a message';
end close cur_helpdesk deallocate cur_helpdesk end END
merge into A using B on B.col1 = A.col1 when Matched then update set B.col1 = A.col1 when not Matched then insert (col1,col2,col3) values (select col1, col2, col3 from A)
merge into A using B on B.col1 = A.col1 when Matched then update set B.col1 = A.col1 when not Matched then insert (col1,col2,col3) values (select col1, col2, col3 from A where col3 >80)
--创建测试表 CREATE TABLE t_A (MCLASS VARCHAR(20),SNO VARCHAR(20)) INSERT INTO t_A SELECT 'L-1','123' UNION ALL SELECT 'L-2','234' UNION ALL SELECT 'L-3','345'
CREATE TABLE t_B (MCLASS VARCHAR(20),SNO VARCHAR(20)) INSERT INTO t_B SELECT 'L-1','999' UNION ALL SELECT 'L-2','999' UNION ALL SELECT 'L-4','888'
--确定目标表 merge into t_A as a --从t_B表中查找MCLASS相同的数据 using t_B as b on a.MCLASS=b.MCLASS and a.mclass= 'L-1'--这里写条件 --当MCLASS相同时,则更新t_A中SNO字段 when Matched then update set a.SNO=b.SNO --当t_A表中不存在MCLASS相同的数据时,则从t_B表插入目标表t_A when Not Matched then Insert(MCLASS,SNO) values(b.MCLASS,b.SNO);
select * from t_A select * from t_B drop table t_A drop table t_B
#12
merge into A using B on B.col1 = A.col1 --这里写条件 and a.col3 >80
CREATE TRIGGER Reminder_Helpdesk on dbo.helpdesk after update as begin declare @INCIDENT_ID nvarchar(15) declare @PRIORITY nvarchar(20) declare @PRIORITY_NEW nvarchar(20)
IF UPDATE (PRIORITY) BEGIN
declare cur_helpdesk cursor for select INCIDENT_ID, PRIORITY from DELETED open cur_helpdesk fetch next from cur_helpdesk into @INCIDENT_ID, @PRIORITY while @@FETCH_STATUS =0 begin select @PRIORITY_new=[PRIORITY] from INSERTED where INCIDENT_ID=INCIDENT_ID if (@PRIORITY_new<@PRIORITY) begin insert into UpdateLog(priority) values(@PRIORITY+' '+@PRIORITY_new) end fetch next from cur_helpdesk into @INCIDENT_ID, @PRIORITY end begin EXEC msdb.dbo.sp_send_dbmail
@recipients = 'test@163.com', @message = 'There is message', @subject = 'There is a message';
end close cur_helpdesk deallocate cur_helpdesk end END