如何从Excel表格中在SQL中获取多个输入。

huangapple go评论72阅读模式
英文:

How to take multiple inputs in SQL from an excel sheet

问题

我正在尝试找到一种方法,可以从Excel表中获取多个客户名称,然后在WHERE子句中使用这些名称,以便我可以跟踪这些客户的订单!例如:

Customer_Name | Quantity | Total purchase price

ABC | 100 | 500

XYZ | 200 | 1000

DEF | 300 | 1500

现在,我正在使用一个Excel文件,从中获取数据并编写SQL查询以筛选我需要的数据(原始数据太大了无法加载)。现在我想从同一工作簿中的不同工作表中的一列中获取多个客户名称,这可能会根据用户输入的客户名称而改变!

SELECT Customer_Name, Quantity, Total Purchase Price 
FROM TABLE1
WHERE Customer_Name='ABC'
OR Customer_Name='XYZ'
OR (以此类推);

当我手动输入每个客户时,它可以正常工作,但我希望它能够动态地处理。如果有人输入了1000个客户名称,它可以从查询中获取这些列的输入。我了解一些VBA(曾经在小型项目上工作过),所以如果能够轻松地通过简单的VBA代码实现,我愿意尝试!

谢谢,我是新手,请告诉我如果我需要更好地解释。

最好的问候

英文:

I am trying to find a way to take multiple customer names from an excel sheet to be used in WHERE clause so that I can track the order for those customers!
eg:

Customer_Name | Quantity | Total purchase price

ABC | 100 | 500

XYZ | 200 | 1000

DEF | 300 | 1500

Now, I am using an excel file, from which I am getting data from SQL Server and writing a SQL query to filter out the data I need(original data is very big to load). Now I want to take multiple customer name from a col which is in a different worksheet in the same workbook in excel, which can change depending upon the customers name entered by the user!

SELECT Customer_Name, Quantity, Total Purchase Price 
FROM TABLE1
WHERE Customer_Name='ABC'
OR Customer_Name='XYZ'
OR (and so on);

It works for each Customer when I put it manually but I want it to be dynamic. If someone puts 1000 customer names for example, it takes the input from those columns in the query. I know a bit of VBA(have worked on small projects), so if I can do it easily from a simple VBA code, I am all ears!

Thank you, I am new, please let me know if I can explain it better.

Best regards

答案1

得分: 2

我建议构建一个以逗号分隔的字符串,其中包含客户名称,并使用<code>WHERE IN ()</code>。以下代码假定客户名称位于列A中。此外,假定有一个列标题。从<code>i = 1</code>开始<code>For</code>循环,以包括第一行。

Sub GetCustomerNames()
    Dim lastRow As Long
    Dim i As Long
    Dim s As String
    Dim sSql As String
    
    lastRow = Range("A1").End(xlDown).Row
    
    For i = 2 To lastRow
        s = s & "'" & Cells(i, "A") & "',"
    Next i
    If Len(s) > 0 Then s = Left$(s, Len(s) - 1) 'remove last '
    
    sSql = "SELECT Customer_Name, Quantity, Total_Purchase_Price FROM TABLE1 WHERE Customer_Name IN (" & s & ")"
    
    Debug.Print sSql
End Sub

输出:

SELECT Customer_Name, Quantity, Total_Purchase_Price FROM TABLE1 WHERE Customer_Name IN ('ABC','XYZ','DEF')
英文:

I recommend building a comma separated string with the customer names and using a <code>WHERE IN ()</code>.

The following code assumes the customer names are in column A. Furthermore a column header is assumed. Start the <code>For</code> loop at <code>i = 1</code> to include the first row.

Sub GetCustomerNames()
    Dim lastRow As Long
    Dim i As Long
    Dim s As String
    Dim sSql As String
    
    lastRow = Range(&quot;A1&quot;).End(xlDown).Row
    
    For i = 2 To lastRow
        s = s &amp; &quot;&#39;&quot; &amp; Cells(i, &quot;A&quot;) &amp; &quot;&#39;,&quot;
    Next i
    If Len(s) &gt; 0 Then s = Left$(s, Len(s) - 1) &#39;remove last &#39;
    
    sSql = &quot;SELECT Customer_Name, Quantity, Total_Purchase_Price FROM TABLE1 WHERE Customer_Name IN (&quot; &amp; s &amp; &quot;)&quot;
    
    Debug.Print sSql
End Sub

Output:

SELECT Customer_Name, Quantity, Total_Purchase_Price FROM TABLE1 WHERE Customer_Name IN (&#39;ABC&#39;,&#39;XYZ&#39;,&#39;DEF&#39;)

huangapple
  • 本文由 发表于 2023年5月17日 17:22:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76270508.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定