Reputation: 39
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
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:
To create a new data source click OK
Enter the server name -> select a driver and click Connect
Enter the server name again and click "Use Trust Connection" or enter SQL Login ID and Password (depending on your enviroment) -> Click "Options"
Select your Database and click OK and OK again, so you are back to Choose Data Source window where you also click OK
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.
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.
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.
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.
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