Filter on Output clause sql

2019-01-25 21:04发布

问题:

I am trying to use a filter on an OUTPUT clause in t-sql.

What I want to do is something like this:

Insert into tbl_1(col1,col2)
Output Inserted.col1 into #tbl_temp 
**where col1 > 0**
select col3, col4
from tbl_2

For performance reasons I don't want to use two insert statements.

回答1:

insert into #tbl_temp
select col1
from
  (
    insert into tbl_1(col1,col2) 
    output Inserted.col1
    select col3, col4 
    from tbl_2
  ) as T
where T.col1 > 0