使用存储过程

时间:2016-10-18 12:30:06

标签: sql sql-server sql-server-2008

我想从多个表中插入数据。从表中的那一个是临时表,其中它来自多个数据,即46和47,其字段名称是产生的。但它不是通过多个条件插入另一个表。

这是我的问题:

Insert into #temp
    select Product.Id 
    from Product 
    left outer join In_abc_Product ON In_abc_Product.ID = Product.ID
    where In_abc_Product.ID IS NULL

BEGIN
    select * from #temp

    --Insert data into In_abc_Product where condition is p.Deleted = 'False' or p.Published = 'True' or  VisibleIndividually = 'True'
        Insert into In_abc_Product(ProductId, SolrStatus, IsDeleted, InTime, StoreId,LanguageId) 
        select tmp.productid,1,0,GETDATE(),s.Id,l.Id from  Language l, Store s, #temp tmp left join Incremental_Solr_Product isp on isp.ProductId = tmp.productid
        left join product p on p.id = isp.ProductId  where isp.Id is NULL and p.Deleted = 'False' or p.Published = 'True' or  VisibleIndividually = 'True'

    --Insert data into In_abc_Product where condition is p.Deleted = 'True' or p.Published = 'False' or  VisibleIndividually = 'False'
        Insert into Incremental_Solr_Product(ProductId, SolrStatus, IsDeleted, InTime, StoreId,LanguageId) 
        select tmp.productid,1,0,GETDATE(),s.Id,l.Id from  Language l, Store s, #temp tmp left join Incremental_Solr_Product isp on isp.ProductId = tmp.productid
        left join product p on p.id = isp.ProductId where isp.Id is NULL and p.Deleted = 'True' or p.Published = 'False' or  VisibleIndividually = 'False'

    END
END 

1 个答案:

答案 0 :(得分:0)

- 请检查一下,它可能达到您的要求。

    Insert into #temp
select Product.Id from Product 
LEFT OUTER JOIN In_abc_Product ON In_abc_Product.ID = Product.ID
WHERE In_abc_Product.ID IS NULL

    BEGIN
    select * from #temp

Insert into In_abc_Product(ProductId, SolrStatus, IsDeleted, InTime, StoreId,LanguageId) 
        select tmp.productid,1,0,GETDATE(),(SELECT S.Id FROM Store s),(SELECT l.Id  Language l)
        from   #temp tmp 
        left join Incremental_Solr_Product isp on isp.ProductId = tmp.productid
        left join product p on p.id = isp.ProductId  
        where isp.Id is NULL and p.Deleted = 'False' or p.Published = 'True' or  VisibleIndividually = 'True'


            Insert into Incremental_Solr_Product(ProductId, SolrStatus, IsDeleted, InTime, StoreId,LanguageId) 
        select tmp.productid,1,0,GETDATE(),(select s.Id from Store s),(select l.Id from Language l)
        from   #temp tmp 
        left join Incremental_Solr_Product isp on isp.ProductId = tmp.productid
        left join product p on p.id = isp.ProductId where isp.Id is NULL and p.Deleted = 'True' or p.Published = 'False' or  VisibleIndividually = 'False'

            END
END 
相关问题