Cari Blog Ini

20 Juni 2012

How To Import Data From File Excel To SQL Using OpenRowSet

1. Sample of data
------------------------------------------------------------------
-----code:start
------------------------------------------------------------------
select * from Cabang
------------------------------------------------------------------
-----code:end
------------------------------------------------------------------

Result Messages:
KdCabang Cabang                       
-------- ------------------------------
005      BSD                         
002      JAYAKARTA                   
003      KEBON JERUK                 
004      KELAPA GADING               
007      SURABAYA                    
006      BANDUNG                     
001      PUSAT                       
008      SEMARANG                    

(8 row(s) affected)


2. Check the file excel, that want to use for import.





3. Run this query
------------------------------------------------------------------
-----code:start
------------------------------------------------------------------
SELECT * INTO Temp_Results
    FROM OPENROWSET
    ('Microsoft.Jet.OLEDB.4.0',
    'Excel 8.0;
    Database=D:\Results_1.xls;
    HDR=YES',
    'SELECT KdCabang,Cabang FROM [Results$]')
------------------------------------------------------------------
-----code:end
------------------------------------------------------------------

Result Messages:
(8 row(s) affected)


4. Check the results
in SQL at Table "Temp_Results"
------------------------------------------------------------------
-----code:start
------------------------------------------------------------------
SELECT * FROM Temp_Results
------------------------------------------------------------------
-----code:end
------------------------------------------------------------------

Result Messages:
KdCabang                                              Cabang         
------------------------------------------------------------
5.0                                                   BSD                          
2.0                                                   JAYAKARTA                    
3.0                                                   KEBON JERUK                  
4.0                                                   KELAPA GADING                
7.0                                                   SURABAYA                     
6.0                                                   BANDUNG                      
1.0                                                   PUSAT                        
8.0                                                   SEMARANG                     

(8 row(s) affected)


Source:
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=49926