如何将数据从Excel表更新到基于SQL的数据库?

时间:2019-02-04 14:41:39

标签: sql excel vba

我有一个带有名称,值和日期列的excel表。我想将这些值和日期放入数据库中。我设法用以下代码创建与数据库的连接。这段代码中的策略是使用该名称进入数据库,然后在下一步中使用此名称的唯一标识进入underliyng表。然后使用此ID更新基础表中的值和日期。问题是链接excel表,以便应该从给定名称的excel表中获取值和日期。

Name               Value           Date 

12345_ABC_DefGeh   12345678        01.11.2018

......大约有15个值。

Sub fixing()    
      Dim pReference As String  
      Dim pFixingValue As Double
      Dim pFixingdate As Date
      Dim pReferenceUnderlying As String

       Dim Con As ADODB.Connection
       Dim Q As New AblSQLQuery
       Dim SQL As String

      Dim uniqueid As Long  ' uniqueid is the unique code of this instrument
      Dim uniqueidUnderlying As Long  
      Dim QuoteType As Long
      Dim Numero As Long
      Dim Stmt As ADODB.Recordset
      Dim bInsert As Boolean
      Dim Ins As SR2COM.Instrument
      Dim InsUnderlying As SR2COM.Instrument
      Dim Ins2 As SR2COM.Instrument
       '
     If isDeveloper() = True Then
         pReference = "12345_ABC_DefGeh"
     End If
     '
     Engine.Risque.Initialise
     Pref.GetUserID Operator, OperatorGroup

     Set Con = Engine.xvz.ConnectADO
     ' get uniqueid and set instrument

     uniqueid = Engine.Instrument(pReference).Code
     Set Ins = Engine.Instrument(uniqueid)

      If pReferenceUnderlying <> vbNullString Then
         UniqueidUnderlying = Engine.Instrument(pReferenceUnderlying).Code
      End If

      ' select latest fixing entry

           SQL = "SELECT *" & _
            " FROM ABV_local_fixings a" & _
           " WHERE Code = " & uniqueid

     If SicovamUnderlying <> 0 Then
         SQL = SQL & _
             " AND underlying = " & uniqueUnderlying
     End If

    SQL = SQL & _
             " AND observation = (SELECT MAX(observation)" & _
                                  " FROM ABV_local_fixings b" & _
                                 " WHERE b.code = a.code"
     If UniqueidUnderlying <> 0 Then
         SQL = SQL & _
                                   " AND b.underlying = a.underlying"
     End If
     SQL = SQL & _
                                 ")" & _
           " ORDER BY numero DESC"

     Set Stmt = Con.Execute(SQL)
 '    End If
     Numero = Stmt!Numero
     QuoteType = Stmt!QuoteType
     UniqueidUnderlying = Stmt!Underlying
     Set InsUnderlying = Engine.Instrument(UniqueidUnderlying)

    If pReferenceUnderlying = vbNullString Then
         pReferenceUnderlying = InsUnderlying.Reference
     End If

check if insert or update

     SQL = "SELECT *" & _
            " FROM ABV_local_fixings a" & _
           " WHERE Code = " & uniqueid

    If UniqueidUnderlying <> 0 Then
         SQL = SQL & _
             " AND underlying = " & UniqueidUnderlying
     End If

    SQL = SQL & _
             " AND observation = TO_DATE('" & Format(pFixingdate, "ddmmyyyy") & "', 'DDMMYYYY')"

     Set Stmt = Con.Execute(SQL)

    If Stmt.EOF Then
         bInsert = True
     End If

     ' insert/update fixing

       If bInsert = True Then
         SQL = "INSERT INTO ABV_local_fixings" & _
                         " (numero, code, observation, underlying, value,  quotetype)" & _
                  " VALUES (" & Numero + 1 & _
                          "," & Uniqueid & _
                          ",TO_DATE('" & Format(pFixingdate, "ddmmyyyy")   & "', 'DDMMYYYY')" & _
                          "," & UniqueidUnderlying & _
                          "," & pFixingValue & _
                          "," & QuoteType & _
                          ")"
     Else
         SQL = "UPDATE ABV_local_fixings" & _
                 " SET value = " & pFixingValue & _
               " WHERE numero = " & Numero & _
                 " AND code = " & Uniqueid & _
                 " AND observation = TO_DATE('" & Format(pFixingdate, "ddmmyyyy") & "', 'DDMMYYYY')" & _
                 " AND underlying =" & UniqueidUnderlying
     End If  

 End Sub>

问题是连接excel表,我从以前的类似项目中获取了此代码。但是,进入数据库并到达基础表是完美的工作。我敢肯定,做些小改动就足够了,我花了太多时间,但仍然无法解决问题。

1 个答案:

答案 0 :(得分:0)

使用

insert into table_name values ()

仅需要插入15行。另外,您可以通过复制/粘贴一些字符串值来自动从excel中插入该数据。