我在我的存储过程中使用合并查询。我想在Insert被触发时返回一个标志。即当PK不匹配时。
CREATE PROCEDURE [dbo].[IUD_QtrMinute]
@qtrminlat varchar(5),
@qtrminlng varchar(6),
@callCenter varchar(20),
@areaId int
AS
BEGIN
merge [dbo].[map_qtrmin] as target
using (values(@qtrminlat, @qtrminlng, @areaId, @callCenter))
as source ([qtrmin_lat], [qtrmin_long], [area_id], [call_center])
on (target.qtrmin_lat = @qtrminlat) and (target.qtrmin_long = @qtrminlng)
when matched and @areaId = 0
then delete
when matched then
update
set [area_id] = @areaId
when not matched then --return a bit flag or something
insert([qtrmin_lat], [qtrmin_long], [area_id], [long_length], [call_center])
values(source.qtrmin_lat, source.qtrmin_long, source.area_id, 1, source.call_center);
END
这可能吗?
答案 0 :(得分:3)
您可以使用output
子句,这是一个小例子:
select 1 a, 1 b into #t0
union all select 2, 2
union all select 3, 3
select 1 a, 1 b into #t1
union all select 2, 2
union all select 4, 4
declare @t table (a int, b int);
merge
#t0 t0
using
#t1 t1 on t0.a = t1.a
when matched then
update set t0.b = t0.b
output
inserted.a, inserted.b
into
@t (a, b)
;
select * from @t;
表@t将包含匹配的值。您可以使用它来设置标志变量
if exists(select * from @t) begin
-- ..
end;