Click here to Skip to main content
15,887,435 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using SQL server 2008. I want to insert data from a temp table to database. I am using While loop to insert data from temp table to database table. Now I am facing an issue: object already exist in the database. Please help me to find a proper solution. Thanks in advance


SQL
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


What I have tried:

I tried to drop the temp table
Posted
Updated 12-Apr-16 21:21pm
v3
Comments
Tomas Takac 13-Apr-16 3:10am    
At the beginning of your script you need to check if the table exists and if it does drop it.

1 solution

select * into PDBCompr from cte2

in this line you are again making a table "PDBCompr", but in above code you are selecting records from this table it means its already exist.

so if you want to insert record in this table then you need to change your query like below:
insert into PDBCompr select * from cte2


Ashish
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900