简体   繁体   中英

CROSS APPLY in MERGE - SQL Server

I need to call function in the marge that return number.

And I want to ask about this number in the NOT MATCHED:

MERGE INTO Table AS m
USING (SELECT DISTINCT edb.*, md.F3, mb.F2 as , xp.idDetails
       FROM Table2 edb 
       JOIN Table4 mb ON md.F3 = mb.Id
       CROSS APPLY dbo.Func(edb.E1, edb.E2, edb.E3, edb.E4, edb.E5, edb.E6) xp(idDetails)
       WHERE xp.idDetails = md.Id
         AND edb.P1= 1000) edbTable ON m.R1 = edbTable.F2 AND m.R2 = 2

WHEN MATCHED THEN 
    UPDATE 
        SET 
        .
        .
WHEN NOT MATCHED AND edbTable.idDetails IS NOT NULL THEN
    INSERT 
        .
        .

I try this code, but it failed:

Table-valued function 'Func' cannot have a column alias.

Does this work?

USING (select distinct edb.*, md.F3, mb.F2
       from Table3 md join
            Table4 mb
            on md.F3 = mb.Id cross apply
            (select edb.*, xp.idDetails
             from table2 edb cross apply
                  dbo.Func(edb.E1, edb.E2, edb.E3, edb.E4, edb.E5, edb.E6) xp(idDetails)
             where xp.idDetails = md.Id and
                   edb.P1 = 1000
            ) edb
      )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM