title: Inserting table in SQL server using Cursor
body: <p>I am trying to import an excel file to SQL , the file has 3 columns : Box - Code - Validity
  i am using the following query</p>
  
  <pre><code>USE [Libatel]
  GO
  
  SET ANSI_NULLS ON
  GO
  SET QUOTED_IDENTIFIER ON
  GO
  
  ALTER PROCEDURE [dbo].[GetDataExcel]
  
  as
  
  DECLARE c CURSOR FOR select  Box, Code , Validity FROM
  OPENROWSET('Microsoft.ACE.OLEDB.12.0', 'Excel 12.0;Database=C:\Barcodes.xlsx;HDR=YES',     'SELECT Box, Code , Validity FROM [sheet1$]')
  declare @Code as bigint
  declare @Box as bigint
  declare @Validity as date
  
  begin
  
  
  open c 
  
  fetch next from c into @Code,@Box,@Validity
  
  WHILE @@FETCH_STATUS = 0
  begin
  select @Box = Box,@Code = BarCode,@Validity =ValidityDate from Cards 
  Insert into Cards (BarCode,Box,ValidityDate) values (@Box,@Code,@Validity)
  fetch next from c into @Box,@Code,@Validity
  end
  CLOSE c
  DEALLOCATE c
  
  
  end
  </code></pre>
  
  <p>I am getting the folowing </p>
  
  <pre><code>11155232026    1        2013-05-18  
  
  1       11155232026     2013-05-18  
  
  
  ...
  ...
  </code></pre>
  
  <p>This is the first line always and Box and Code are changing places on each line , what i am doing wrong ?</p>