使用多选查询将数据插入另一个表

时间:2015-10-20 10:20:56

标签: sql sql-server sql-server-2012

我正在使用sql server 2012。

我在下面编写了一个选择查询,可以正常工作。但是,我不确定如何将从查询返回的数据插入另一个表(tblTempPrices)?

我的插入

;insert into tblTempPrices(DateEntry, DatePrice, ISIN, Price, PriceSource, SecurityType, TableCheck)

以下查询有效

with ret as
(
    select distinct ISIN, Price
    from tblFI_Benchmark_R 
    where DateEntry = '2015-10-19'
), 
stat as
(
     select distinct ISIN, Price
     from tblFI_Benchmark_S 
     where DateEntry = '2015-10-19'
), 
allSec as
(
    select * from ret
    union
    select * from stat
)
select '2015-10-20', '2015-10-19', ISIN, Price, 'BARC', 'FixedIncome', 'PCF' from allSec

1 个答案:

答案 0 :(得分:3)

试试这个 -

;with ret as
    (
        select distinct ISIN, Price
        from tblFI_Benchmark_R 
        where DateEntry = '2015-10-19'
    ), 
    stat as
    (
         select distinct ISIN, Price
         from tblFI_Benchmark_S 
         where DateEntry = '2015-10-19'
    ), 
    allSec as
    (
        select * from ret
        union
        select * from stat
    )
    insert into tblTempPrices(DateEntry, DatePrice, ISIN, Price, PriceSource, SecurityType, TableCheck)
    select '2015-10-20', '2015-10-19', ISIN, Price, 'BARC', 'FixedIncome', 'PCF' from allSec;