1

我正在执行以下步骤:

  • 合并语句。如果值不存在,merge 语句会将值插入到视图中。
  • 选择stmt。选择我插入的记录

但是选择查询返回空白。示例代码如下:

declare @now AS DATETIME = GETUTCDATE(),
        @uuid AS INT = 1;

MERGE A.V_Table AS T
(
  Select 
     A1,
     A2
  FROM B.Table
)S
on T.A1 = T1.A1
   and T.A2 = T1.A2
when not matched by target then
insert
(
  A1,
  A2,
  UpdateUUID,
  UpdateTimeStamp
)
values
(
 s.a1,
 s.a2,
 @uuid,
@now
);

select *
from A.V_Table
where updateuUid = @uuid and UpdateTimeStamp = @now;

如果我打印出@now 和@uuid 的值并在选择查询中使用这些值,则返回记录。但不是当它像上面那样执行时。

知道为什么会这样吗?选择查询不是只有在合并完成后才运行吗?

4

1 回答 1

0

您所缺少的只是合并过程后的“GO”关键字:

declare @now AS DATETIME = GETUTCDATE(),
        @uuid AS INT = 1;

MERGE A.V_Table AS T
(
  Select 
     A1,
     A2
  FROM B.Table
)S
on T.A1 = T1.A1
   and T.A2 = T1.A2
when not matched by target then
insert
(
  A1,
  A2,
  UpdateUUID,
  UpdateTimeStamp
)
values
(
 s.a1,
 s.a2,
 @uuid,
@now
); 
GO

select *
from A.V_Table
where updateuUid = @uuid and UpdateTimeStamp = @now;
于 2018-02-13T13:50:22.900 回答