Excel Vlookup针对SQL数据库

时间:2018-06-21 08:32:48

标签: vba tsql

我通常根据另一个工作簿中的表(数据库)对数据进行查找。 (Excel到Excel –通常每个人都这样做)。 由于我的表(数据库)增长了140万行,因此我需要将其传输到MS SQL数据库中的SQL表。 我不能仅将Vlookup的常规excel文件移动到SQL。 我如何针对SQL表vlookup excel。 使用Visual Basic或TSQL满足要求的任何解决方案。 谢谢

3 个答案:

答案 0 :(得分:0)

是的,显然,限制仅超过一百万行,因此即使行总数超过一百万,您也可以使用Power Pivot表连接到多个CSV文件,汇总所需的日期,并且将所有内容合并到一个工作表中。

enter image description here

有关如何执行此操作的更多想法,请参见下面的链接。

https://powerpivotpro.com/2017/01/import-csv-files-folder-filenames-excel/

http://sfmagazine.com/post-entry/january-2016-excel-combining-many-csv-files/

https://support.office.com/en-us/article/create-a-pivottable-with-an-external-data-source-db50d01d-2e1c-43bd-bfb5-b76a818a927b

答案 1 :(得分:0)

是的,您应该能够做到:

搜索“ excel connect to sql server”会带来结果(描述如何连接到sql数据源,在此不多提及)

https://support.office.com/en-us/article/connect-a-sql-server-database-to-your-workbook-power-query-22c39d8d-5b60-4d7e-9d4b-ce6680d43bad

然后,当您建立连接时,可以在VBA中编写自己的查找功能,随心所欲。

答案 2 :(得分:-1)

您将使用'Where'子句!

Sub ImportFromSQLServer()

Dim Cn As ADODB.Connection
Dim Server_Name As String
Dim Database_Name As String
Dim User_ID As String
Dim Password As String
Dim SQLStr As String
Dim RS As ADODB.Recordset
Set RS = New ADODB.Recordset

Server_Name = "your_server_name_here"
Database_Name = "your_DB_name_here"
'User_ID = "******"
'Password = "****"

SQLStr = "select * from dbo.TBL Where EMPID = '2'" 'and PostingDate = '2006-06-08'"

Set Cn = New ADODB.Connection
Cn.Open "Driver={SQL Server};Server=" & Server_Name & ";Database=" & Database_Name & ";"
'& ";Uid=" & User_ID & ";Pwd=" & Password & ";"

RS.Open SQLStr, Cn, adOpenStatic

    With Worksheets("Sheet1").Range("A1")
        .ClearContents
        .CopyFromRecordset RS
    End With

RS.Close
Set RS = Nothing
Cn.Close
Set Cn = Nothing

End Sub