3

我正在使用 SQL Server 2008 并merge into从表值参数中获取我的数据。

 CREATE PROCEDURE [dbo].[SP1]
    (
    @p as TTVP readonly    
    )

AS
declare @ThisId int
Begin

     Merge into [dbo].[FIRSTTABLE] tp
     Using @p ps
     on tp.ID = ps.ID

     When Matched Then
     Update
     set tp.NAME = ps.NAME,
     tp.myDATE = Getdate()

     When Not Matched Then

     insert (NAME, myDATE)
     Values (ps.NAME, getDate());

     seclect @ThisId = identity_scope()

     insert into SECONDTABLE (ID,  myDATE) 
     select (@ThisId,  Getdate())

  END  

我想我从来没有从我的陈述中scope_identity得到身份证。insert

所以我的第一次更新和插入有效,但是如何获得第二次插入的插入 ID?

4

1 回答 1

3

这显示了如何从 MERGE 语句中记录 OUTPUT 数据。请注意,OUTPUT 子句跨越 MERGE 中的所有分支,包括 UPDATE 和 INSERT 部分。然后它使用inserted.id 和deleted.id 之间的不匹配来计算实际插入的内容。OUTPUT 子句显示了如何nameinserted虚拟表中继承列。

use tempdb;

-- create the test tables and table type
create table dbo.FirstTable (
    ID int identity primary key,
    NAME sysname,
    myDATE datetime);
GO
create table dbo.SecondTable (
    ID int primary key,
    myDATE datetime,
    NAME sysname);
GO
create type TTVP as TABLE(ID int, NAME sysname);
GO

-- create the procedure
CREATE PROCEDURE dbo.SP1
    @p as TTVP readonly
AS
SET NOCOUNT ON;

create table #cache (new_id int primary key, old_id int, name sysname);

merge into dbo.firsttable tp
using @p ps on tp.id = ps.id
when matched then
    update
       set tp.name = ps.name,
           tp.mydate = getdate()
when not matched then
    insert (name, mydate)
    values (ps.name, getdate())
output inserted.id, deleted.id, inserted.name into #cache;

insert into dbo.secondtable (id, mydate, name)
select new_id, getdate(), name
from #cache
where old_id is null;
GO

-- set up some test data (2 rows)
truncate table dbo.FirstTable;
insert dbo.FirstTable values ('abc', getdate());
insert dbo.FirstTable values ('ghi', getdate()-1);
GO

-- execute the procedure
declare @p TTVP;
insert @p values (1, 'abc'),(2, 'xyz'),(3, 'def');
exec dbo.SP1 @p;

-- check the results
select * from dbo.firsttable
select * from dbo.secondtable
于 2013-04-05T01:25:21.680 回答