如何更新从Excel表中的数据为基于SQL数据库?

问题描述 投票:0回答:1

我有一个名称,值和日期列Excel表格。我希望把这些值和日期在数据库中。我设法创建数据库使用下面的代码连接。在此代码的策略是去里面数据库的名称,然后在下一步去underliyng表这个名字的UNIQUEID。然后,在基础表这个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表格我把这个代码,以前类似的项目。但它很好地工作,进入数据库,并达到对基础表。我相信小的修改就足够了,我花了太多的时间,但仍然未能解决的问题。

sql excel vba
1个回答
0
投票

使用

insert into table_name values ()

它只有15,你需要插入行。此外,您可以用比特字符串值的复制/粘贴自动从Excel中插入。

© www.soinside.com 2019 - 2024. All rights reserved.