已有一个名为' PDBCompr'在数据库中

时间:2016-04-13 05:56:36

标签: sql-server-2008 loops temp-tables

我正在使用SQL Server 2008.我想将数据从temp table插入database。我正在使用While loop将数据从临时表插入到数据库表中。 现在我面临一个问题:

  

对象已存在于数据库中。

 declare @rev as int  ,
    @sQuotationNo NVARCHAR(15),                                                                                      
    @sQRevNo int                                                                                               
    set @rev=(select top 1 QRevNo from PDBCompr Where QuotationNo='JCS_G1415_008' and QRevNo<>'3' order by QRevNo desc)   

    ;with cte as   
    (  
    SELECT    
     ROW_NUMBER() OVER(ORDER BY QuotationNo) AS sSLNO,                                                 
     [CompanyCode] ,      
     [ProjectCode] ,   
     [PRevNo],  
     [CSlNo],      
     [ComprDescription] ,      
     [PID] ,      
     [RatingCode] ,      
     [Rating] ,      
     [StdSystems] ,      
     [BoosterSystems] ,      
     [GCUSystems] ,      
     [KOFSystems] ,      
     [HeaterSystems] ,      
     [OtherSystems] ,      
     [Comments] ,      
     [Currency1] ,      
     [UnitPrice1] ,      
     [Currency2] ,      
     [ExchRate2] ,      
     [UnitPrice2] ,      
     [Currency3],      
     [ExchRate3] ,      
     [UnitPrice3] ,      
     [CreateId] ,      
     [CreateDate] ,      
     [UpdateId] ,      
     [UpdateDate]                                                                                         
    from PDBCompr         
    where QuotationNo='JCS_G1415_008' and CompanyCode ='001' and QRevNo ='2'               
    and AddCmprId not in (select distinct AddCmprId from PDBCompr where QuotationNo='JCS_G1415_008' and CompanyCode ='001' and QRevNo ='3'   )  
    )      

    select * into #temp from cte   

    declare @cnt int , @loopCnt int=1 
    select @cnt =( select COUNT(*) from #temp)  

     while (@loopCnt<=@cnt)
     begin

    ;with cte2 as 
    (
     SELECT    
     sSLNO,                                                 
     [CompanyCode] ,      
     [ProjectCode] ,           
     (select Max(PRevNo)+1 from PDBCompr) [PRevNo],      
     (select Max(CSlNo) +1 from PDBCompr)[CSlNo],      
     [ComprDescription] ,      
     [PID] ,      
     [RatingCode] ,      
     [Rating] ,      
     [StdSystems] ,      
     [BoosterSystems] ,      
     [GCUSystems] ,      
     [KOFSystems] ,      
     [HeaterSystems] ,      
     [OtherSystems] ,      
     [Comments] ,      
     [Currency1] ,      
     [UnitPrice1] ,      
     [Currency2] ,      
     [ExchRate2] ,      
     [UnitPrice2] ,      
     [Currency3],      
     [ExchRate3] ,      
     [UnitPrice3] ,      
     [CreateId] ,      
     [CreateDate] ,      
     [UpdateId] ,      
     [UpdateDate]                                                                                        
      from #temp where sSLNO=@loopCnt
      )      

    select * into PDBCompr from cte2
    set @loopCnt= @loopCnt+1
    drop table  #temp
      end

请帮我找一个合适的解决方案。提前致谢

1 个答案:

答案 0 :(得分:0)

  

从cte2

中选择*进入PDBCompr

在这一行中,你再次创建一个表“PDBCompr”,但在上面的代码中,你从这个表中选择记录,这意味着它已经存在。

因此,如果您想在此表中插入记录,则需要更改您的查询,如下所示:

  

插入PDBCompr select * from cte2