【问题标题】:EXCEL VBA to SQL Index and SeekEXCEL VBA to SQL Index and Seek
【发布时间】:2016-11-30 19:01:26
【问题描述】:

我正在将 Excel 表中的数据导出到 SQL Server 数据库,如果存在 UPDATE 否则 INSERT。

以下 VBA 代码适用于导出到 ACCESS 数据库, 但不是 SQL SERVER 数据库表。

出现错误消息:.Index 和 .Seek 的属性使用无效。

请帮忙!!!呵呵

Sub ExcelDataToSql ()

Dim cn As ADODB.Connection, rs As ADODB.Recordset, r As Long
Dim lastrow As Long, o As Long

Set cn = New ADODB.Connection
Set rs = New ADODB.Recordset

cn.Open "Provider=SQLNCLI11;Server=***;Database=****;Trusted_Connection=yes;"
rs.CursorLocation = adUseServer
rs.Open "InventorySQL", cn, 1, 3, adCmdTableDirect

' Get Lastrow
Worksheets("InventoryEXCEL").Select
lastrow = Worksheets("InventoryEXCEL").Cells(rows.Count, 1).End(xlUp).Row
r = 2 ' the start row in the worksheet
For o = 2 To lastrow

    'Check For Duplicate In Database SQL
    With rs
        .Index = "PrimaryKey"
        .Seek Range("A" & r).Value

        If .EOF Then
            .AddNew            
            'If No Duplicate insert New Record
            rs.Fields("oPartno") = Range("A" & r).Value
            rs.Fields("oDesc") = Range("B" & r).Value
            rs.Fields("oCost") = Range("C" & r).Value
        .update
        Else          
            ' If Duplicate Found Update Existing Record
            rs.Fields("oDesc") = Range("B" & r).Value
            rs.Fields("oCost") = Range("C & r).Value
            .Update
        End If
    End With

Next o
    rs.Close
    Set rs = Nothing
    cn.Close
    Set cn = Nothing
    MsgBox "Posting Completed"
End Sub

。 Index = "PrimaryKey" --- Sysntax 错误:无效使用属性 .Seek Range ("A" & r).Value Sysntax Error :

【问题讨论】:

  • 为什么 r 和 mysal 被标记在这里?如果我没有误解,SQL Server 似乎是更合适的标签?
  • 感谢您的反馈...这是我在stackoverflow中的第一篇文章...以后会更加小心...我道歉。

标签: sql-server vba excel


【解决方案1】:

参考:Seek Method and Index Property Example (VB)

MSDN 示例传递一个数组作为第一个参数。

rstEmployees.Seek Array(strID), adSeekFirstEQ

第一个参数的名字 os KeyValues 也意味着一个数组

我会先试试这个

.Seek Array(Range("A" & r).Value)

使用SeekEnum 值之一也可能有益

更新: TOH OP 发现这是相关代码 sn-p

MSDN 还建议检查 Provider 是否支持 .Index.Seek

If rstEmployees.Supports(adIndex) And rstEmployees.Supports(adSeek) Then

【讨论】:

  • 感谢您的帮助。看来 Provider 不支持 .index 和 .seek 属性和方法。
  • 感谢您的更新。我修改了答案。
  • 试试你的方法 .seek array(range("A" & r).valuye, seekEnum... 运行时错误 3251 - 当前提供程序不支持索引功能所需的接口。
  • @Toh 你知道ConnectionStrins.Com吗?他们有完整的提供商列表。
  • 是的...我使用 ConnectionStrins.Com 的提供程序。如何知道哪个提供者为 .index 和 .seek 提供功能。当前使用的连接没有连接问题。
【解决方案2】:

我的问题已通过解决方法解决。

许多资源表明 Sql Providers 不支持索引和查找功能。所以我避免使用 Index and Seek

我通过将 excel 工作表作为源表导入 Sql 服务器来解决...然后将目标表与源表合并...如果匹配 UPDATE,如果不匹配 INSERT。

select * from InventoryTableSQL
select * from InventoryTableFromExcel

Merge InventoryTableSQL as T
using InventoryTableFromExcel as S
on t.oPartno = s.oPartno
when matched then
    update set t.oPartno = s.oPartno,
    t.oDesc = s.oDesc,
    t.oCost = s.oCost
when not matched by target then
    insert(oPartno, oDesc, oCost) values(s.oPartno, s.oDesc, s.oCost));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-08
    • 1970-01-01
    • 2016-10-23
    • 2021-02-23
    • 2018-12-30
    • 1970-01-01
    • 2021-01-02
    • 1970-01-01
    相关资源
    最近更新 更多