使用存储过程从临时表插入数据



我想从多个表中插入数据。其中一个表是临时表,其中包含多个数据,即字段名为productid的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 

——请检查一下,它可能达到你的要求。

    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 

最新更新