Tom Repetti
Tom Repetti

Reputation: 39

check if excel cell value exists in SQL

I have an Excel worksheet that contains part numbers for an order. I want to query the SQL table of parts to see if each part already exists and, if not, display "New" in a cell. I already do this by importing the part_number column from sql to excel then use IFERROR on a VLOOKUP to display the message.

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.

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? Would need to iterate through each line in the excel worksheet, or could I do it more 'set' oriented?

Upvotes: 0

Views: 3751

Answers (1)

Peter Liapin
Peter Liapin

Reputation: 1275

You can create a dynamic query with parameters in Excel to pull only the part numbers relevant to your order. Here is how you do it:

  1. Go to Data tab -> Get Data -> From Other Sources -> From Microsoft Query:

Excel, get data from 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:

To create a new data source click OK

Excel, Microsoft Query, create new SQL Server datasource

Enter the server name -> select a driver and click Connect

Excel, Microsoft Query, connect

Enter the server name again and click "Use Trust Connection" or enter SQL Login ID and Password (depending on your enviroment) -> Click "Options"

Excel, Microsoft Query, database options

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, select existing datasource

  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 Wizard - Choose Columns

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.

Note: I mentioned that Microsoft Query does not handle nvarchar(MAX) type well. 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.

  1. Go to Data tab -> Properties -> Connection Properties -> Definition tab. Here you can edit Command Text:

Microsoft Query: Change Command Text

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.

  1. Click OK and Connection Properties -> Definition again (required to enable "Parameters..." button) -> click "Parameters..."

Microsoft Query: Parameters

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.

  1. Now go to that empty cell which you used as a parameter and enter the following formula: =TEXTJOIN(",",TRUE,[PartNumberValuesRange]). [PartNumberValuesRange] must be changed to a range which have all the part numbers from your order.

  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.

At this point you can use your VLOOKUP + IFERROR approach to display a corresponding message. 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. This should speed up your workflow as you do not need to pull the entire table from the database now.

Upvotes: 2

Related Questions