从 Excel 2007 查询 SQL 并返回多个值

发布于 2024-10-17 17:09:45 字数 554 浏览 2 评论 0原文

我正在尝试获取少量数据(Excel 中大约 200 个字段),并使用每个项目的 where 子句中的该字段从 SQL 检索数据。

TABLE:

ID  Name    Phone
1   Test1   1234
2   Test2   1235
3   Test3   1236


Excel:
Date   ID
2/1/11 1
2/1/11 2
2/1/11 3

我希望能够在 Excel 中进行检索(希望本身不需要编写任何额外的代码 - 也许只是一个带有查询的 Excel ODBC 或 SQL 连接。所以我的数据最终会在 Excel 文档中这样显示:

Excel:
Date   ID  Name    Phone 
2/1/11 1   Test1   1234
2/1/11 2   Test2   1235
2/1/11 3   Test3   1236

我不确定如果我解释得足够清楚的话...

我正在使用 Excel 2007,并且我也有 2010 SQL 是 SQL Server 2000。

谢谢!

I'm attempting to take a small amount of data, about 200 fields in Excel and retreive data from SQL with that field in the where clause for each item.

TABLE:

ID  Name    Phone
1   Test1   1234
2   Test2   1235
3   Test3   1236


Excel:
Date   ID
2/1/11 1
2/1/11 2
2/1/11 3

I want to be able to retrieve, within excel (hopefully without writing any additional code per se - maybe a simply Excel ODBC or SQL connection with a query. So my data would end up as such on the Excel Document:

Excel:
Date   ID  Name    Phone 
2/1/11 1   Test1   1234
2/1/11 2   Test2   1235
2/1/11 3   Test3   1236

I'm not sure if I'm explaining myself clearly enough....

I'm using Excel 2007 and I also have 2010 laying around somewhere. SQL is SQL Server 2000.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

欢你一世 2024-10-24 17:09:46

阿杜,恐怕。

Dim cn As Object
Dim rs As Object
Dim strFile As String
Dim strCon As String
Dim strSQL As String
Dim s As String
Dim i As Integer, j As Integer

''This is not the best way to refer to the workbook
''you want, but it is very convenient for notes
''It is probably best to use the name of the workbook.

strFile = ActiveWorkbook.FullName

''Note that if HDR=No, F1,F2 etc are used for column names,
''if HDR=Yes, the names in the first row of the range
''can be used.
''This is the Jet 4 connection string, you can get more
''here : http://www.connectionstrings.com/excel

strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strFile _
    & ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";"

''Late binding, so no reference is needed

Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")


cn.Open strCon

strSQL = "SELECT * " _
       & "FROM [Sheet1$] a " _
       & "LEFT JOIN " _
       & "[ODBC;Driver={SQL Server Native Client 10.0};" _
       & "Server=servername;Database=test;" _
       & "Trusted_Connection=yes].tbl b " _
       & "ON a.[Id]=b.[Id] "

rs.Open strSQL, cn, 3, 3


''Pick a suitable empty worksheet for the results
Worksheets("Sheet3").Cells(2, 1).CopyFromRecordset rs

''Tidy up
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing

ADO, I am afraid.

Dim cn As Object
Dim rs As Object
Dim strFile As String
Dim strCon As String
Dim strSQL As String
Dim s As String
Dim i As Integer, j As Integer

''This is not the best way to refer to the workbook
''you want, but it is very convenient for notes
''It is probably best to use the name of the workbook.

strFile = ActiveWorkbook.FullName

''Note that if HDR=No, F1,F2 etc are used for column names,
''if HDR=Yes, the names in the first row of the range
''can be used.
''This is the Jet 4 connection string, you can get more
''here : http://www.connectionstrings.com/excel

strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strFile _
    & ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";"

''Late binding, so no reference is needed

Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")


cn.Open strCon

strSQL = "SELECT * " _
       & "FROM [Sheet1$] a " _
       & "LEFT JOIN " _
       & "[ODBC;Driver={SQL Server Native Client 10.0};" _
       & "Server=servername;Database=test;" _
       & "Trusted_Connection=yes].tbl b " _
       & "ON a.[Id]=b.[Id] "

rs.Open strSQL, cn, 3, 3


''Pick a suitable empty worksheet for the results
Worksheets("Sheet3").Cells(2, 1).CopyFromRecordset rs

''Tidy up
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
檐上三寸雪 2024-10-24 17:09:46

除非有充分的理由不在代码中执行此操作,否则您应该只使用代码而不是 sql。

Unless there is a good reason to not do this in code, you should just use code instead of sql.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文