简体   繁体   English

检查SQL中是否存在excel单元格值

[英]check if excel cell value exists in SQL

I have an Excel worksheet that contains part numbers for an order.我有一个 Excel 工作表,其中包含订单的部件号。 I want to query the SQL table of parts to see if each part already exists and, if not, display "New" in a cell.我想查询零件的 SQL 表以查看每个零件是否已经存在,如果不存在,则在单元格中显示“新建”。 I already do this by importing the part_number column from sql to excel then use IFERROR on a VLOOKUP to display the message.我已经通过将 part_number 列从 sql 导入到 excel 然后在 VLOOKUP 上使用 IFERROR 来显示消息来做到这一点。

As the sheet grows larger and more workers are remote over VPN, the time it takes to refresh the part_number download is becoming an issue.随着工作表越来越大,越来越多的工作人员通过 VPN 远程访问,刷新 part_number 下载所需的时间正在成为一个问题。

Is there a way to use a sql query from Excel, using the past_number cell, to return a value I can test with an IF or something else?有没有办法使用来自 Excel 的 sql 查询,使用 past_number 单元格,返回一个我可以用 IF 或其他东西测试的值? Would need to iterate through each line in the excel worksheet, or could I do it more 'set' oriented?需要遍历 excel 工作表中的每一行,还是我可以更“设置”导向?

You can create a dynamic query with parameters in Excel to pull only the part numbers relevant to your order.您可以在 Excel 中使用参数创建动态查询,以仅提取与您的订单相关的部件号。 Here is how you do it:这是你如何做到的:

  1. Go to Data tab -> Get Data -> From Other Sources -> From Microsoft Query :转到数据选项卡 ->获取数据->从其他来源->从 Microsoft Query

Excel,从 Microsoft Query 获取数据

  1. Select New Data Source or select an existing one if you already had your SQL server data source created before -> click OK:如果您之前已经创建了 SQL Server 数据源,请选择新数据源或选择现有数据源 -> 单击确定:

To create a new data source click OK要创建新数据源,请单击“确定”

Excel、Microsoft Query、创建新的 SQL Server 数据源

Enter the server name -> select a driver and click Connect输入服务器名称 -> 选择一个驱动程序并点击连接

Excel、Microsoft Query、连接

Enter the server name again and click "Use Trust Connection" or enter SQL Login ID and Password (depending on your enviroment) -> Click "Options"再次输入服务器名称并单击“使用信任连接”或输入 SQL登录 ID密码(取决于您的环境)-> 单击“选项”

Excel、Microsoft Query、数据库选项

Select your Database and click OK and OK again, so you are back to Choose Data Source window where you also click OK选择您的数据库,然后再次单击“确定”和“确定” ,因此您将返回“选择数据源”窗口,您还单击“确定”

Excel、Microsoft Query,选择现有数据源

  1. On the Query Window - Choose Columns window select your PartNumbers table and a column where you store part number's name (or whatever you use to identify it in the order):Query Window - Choose Columns窗口中,选择您的 PartNumbers 表和一列,您可以在其中存储零件编号的名称(或用于在订单中识别它的任何名称):

查询向导 - 选择列

Click Next 3 times -> Finish (leave everything as it is by default) -> Select where to put the results and Click OK on the Import Data window.单击下一步3 次 ->完成(默认情况下保留所有内容)-> 选择放置结果的位置,然后在导入数据窗口中单击确定

Note: I mentioned that Microsoft Query does not handle nvarchar(MAX) type well.注意:我提到 Microsoft Query 不能很好地处理nvarchar(MAX)类型。 In my tests if I have a column with nvarchar(MAX) I would see an empty values for that column when I import it into an excel worksheet.在我的测试中,如果我有一个带有 nvarchar(MAX) 的列,当我将它导入到 Excel 工作表中时,我会看到该列的空值。

  1. Go to Data tab -> Properties -> Connection Properties -> Definition tab.转到数据选项卡 ->属性->连接属性->定义选项卡。 Here you can edit Command Text :在这里您可以编辑命令文本

Microsoft Query:更改命令文本

Change it to something like:将其更改为:

SELECT PartNumbers.[Name] as Name
FROM TestDB.dbo.PartNumbers PartNumbers 
WHERE PartNumbers.[Name] in (SELECT value FROM STRING_SPLIT(CAST(? as nvarchar(100)), ','))

Note , we have a question mark in the SQL query above - it will be your dynamic parameter to filter part numbers.请注意,我们在上面的 SQL 查询中有一个问号 - 它将是您过滤零件号的动态参数。

  1. Click OK and Connection Properties -> Definition again (required to enable "Parameters..." button) -> click "Parameters..."再次单击“确定”和“连接属性” ->“定义” (需要启用“参数...”按钮)-> 单击“参数...”

Microsoft Query:参数

On the Parameters window select "Get the value from the following cell" and select a cell which you will use later to add a dynamic value for the filter (it can be empty for now).在“参数”窗口中,选择“从以下单元格获取值”并选择一个单元格,稍后您将使用该单元格为过滤器添加动态值(现在可以为空)。

You can also check "Refresh automatically when cell value changes" option.您还可以选中“单元格值更改时自动刷新”选项。 Then click OK button on all opened windows, so you are back to excel table which now should have no rows.然后在所有打开的窗口上点击 OK 按钮,这样你就回到了现在应该没有行的 excel 表。

  1. Now go to that empty cell which you used as a parameter and enter the following formula: =TEXTJOIN(",",TRUE,[PartNumberValuesRange]) .现在转到您用作参数的空单元格并输入以下公式: =TEXTJOIN(",",TRUE,[PartNumberValuesRange]) [PartNumberValuesRange] must be changed to a range which have all the part numbers from your order. [PartNumberValuesRange] 必须更改为包含您订单中所有零件号的范围。

  2. Now, if you did everything right you should see how the existing part number records pulled from the database to your table you made during the steps 1-5.现在,如果您做的一切正确,您应该会看到现有零件号记录如何从数据库中提取到您在步骤 1-5 中创建的表中。

At this point you can use your VLOOKUP + IFERROR approach to display a corresponding message.此时你可以使用你的VLOOKUP + IFERROR方法来显示相应的消息。 The table you reference in the formula will have only a subset of part numbers which exist in both the order in your excel and in the database, so you can easily find all the part numbers which are missing in the database.您在公式中引用的表格将只有零件编号的子集,这些零件编号以 Excel 和数据库中的顺序存在,因此您可以轻松找到数据库中缺少的所有零件编号。 This should speed up your workflow as you do not need to pull the entire table from the database now.这应该会加快您的工作流程,因为您现在不需要从数据库中提取整个表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM