0

I'm trying to get data from both external source and another sheet of my excel file into one table. What I have in SQL Server is a table of my customers: ╔════╦══════╦═════════╗ ║ ID ║ Name ║ Country ║ ╠════╬══════╬═════════╣ ║ 1 ║ Joe ║ Spain ║ ║ 2 ║ Bob ║ Frence ║ ║ 3 ║ Eva ║ Spain ║ ╚════╩══════╩═════════╝ What I have in another sheet of my excel file is a list of countries and taxes: ╔═════════╦═════╗ ║ Country ║ VAT ║ ╠═════════╬═════╣ ║ Germany ║ 19% ║ ║ Frence ║ 20% ║ ║ Spain ║ 21% ║ ╚═════════╩═════╝ And what I need in the end is a table which contains my customer and the tax which he should pay. The table should be in one of the sheets of my excel document and user should be able to easily update it on demand. ╔══════════╦═════╗ ║ Customer ║ VAT ║ ╠══════════╬═════╣ ║ Joe ║ 21% ║ ║ Bob ║ 20% ║ ║ Eva ║ 21% ║ ╚══════════╩═════╝ Any idea what would be a nice and easy solution?

I tried two excel sql queries - one from SQL Server and second one "from microsoft query" (from the excel file). But since I cannot run them subsequenty, I cannot make sure the data after one update is correct.

I also tried one sql queries (from SQL Server) and a VLOOKUP formula. But the formula is not automatically calculated after the table is filled in.

7
  • As I understand you got two tables - one on SQL Server side another on Excel sheet and you want to join them on another Excel sheet. Am I right? You also write that user will enter some data (in Excel I suppose) and join table should be updated on demand - some button will be pressed or how? Commented Jun 17, 2016 at 7:58
  • Why dont import all the sheet to SSMS and then use SQL to do what you want. Commented Jun 17, 2016 at 8:05
  • So you want to pull customer data from SQL when you open your workbook (thus acquiring new customers) then rebuild your customer vat worksheet and optionally rebuild the customer vat worksheet on a customer refresh? Commented Jun 17, 2016 at 8:07
  • 1
    You could upload the new data to SQL and get it all, you could use PowerQuery, you could get the SQL data, paste it and then get the other data and paste it, you could convert the sheet data to a TempTable or a TableVariable on SQL and then get all together. These are just a few options. The solution depend on the requirements and options (speed, master data management, amount of data, connection speed to SQL, etc.). So, please be a bit more specific. The post is (in its current state) extremely broad. Commented Jun 17, 2016 at 8:22
  • @gofr1, yes, you understand it correctly. The user is able to change the excel table - the Country-VAT table. Regarding the "update on demand" - if I use the excel out-of-the-box facility "Data from Other Sources", the button "Refresh Data" is already there. Commented Jun 18, 2016 at 11:36

1 Answer 1

3

My comment above was merely intended to open a few possibilities which you might have at hand.

PowerQuery is a free Plug-In for Office 2013. In Office 2016 it is part of Excel and no longer a Plug-In.

Anyway, you seem to prefer the approach to use a temp table or a table variable on your SQL. Hence, I will elaborate a bit more on this approach:

In the end you will want a query similar to this one:

set nocount on;
declare @tblVAT table
    (
     Country nvarchar(50),
     VAT decimal(9, 7)
    );

insert  into @tblVAT
        (Country, VAT)
values  (N'Germany', 0.19),
        (N'Frence', 0.20),
        (N'Spain', 0.21);


select  tc.Name,
        tc.ID,
        case when tc.Country is null then tv.Country
             else tc.Country
        end as Country,
        tv.VAT
from    dbo.tblCustomers as tc
full outer join @tblVAT as tv
on      tv.Country = tc.Country;

Note the importance of set nocount on; at the start of the above SQL query. Without that it will not work!

Once, you have this query you can simply paste it into Excel using the menu DataGet External DataFrom SQL Server. In a first step you will get the customer table and then in a second step refine the query to include the table variable as described above. Here is a short visual summary:

enter image description here

I guess, at this point the only remaining questions are:

  1. How do you dynamically create the above SQL statement and
  2. how do you get the above table in Excel then updated with this updated SQL statement.

To dynamically create the above SQL you might want to have a look at the following: Using an array or dictionary as from clause in sql in excel vba

Just yesterday I answered a similar question where a user wanted to pass the content of an Excel sheet as a dynamically created table to an SQL Server. You can easily adapt (or even use it as is) for your purpose.

For the last step (update this table in Excel with this new SQL query) you can use the macro recorder and do what I did in the above screenshot. The automatically created code is nothing more / less than what I would propose to you.

So, there you have it. Let me know if I wasn't clear enough or if you require further details to understand this solution.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.