0

I am trying to do a report for order number which are been refunded, I have a list of them in Excel, but I want to know more about that orders like amount, reason for refund and so on. All that information is stored in a SQL Server table.

It is lot of laborious work to do one by one, is there a way so I can check them in one go? For now I am using where clause but it is taking ages to check one by one.

Please help.

4
  • So you want a list of all orders which have been refunded and export them to excel? Could you give us some more information? For example what your database table looks like. Commented May 3, 2017 at 11:08
  • I think you want to import the Excel sheet into an SQL table and query it joined with the existing SQL orders table, right? Commented May 3, 2017 at 11:13
  • Hi, I have only one column in excel [ Order number ] and In sql- Refund orders table I have [order number] , [amount], [reason],[country],[date]. I do not need all the order numbers in sql, I want to match orders numbers which are in excel. Commented May 3, 2017 at 11:15
  • That's ok. Will still work. Commented May 3, 2017 at 11:17

1 Answer 1

1

Seeing as you haven't provided any data model, etc. I have made some assumptions here. One way to do this would be to create a temporary table, populate it, and then join this to the tables you want to query.

The first step is to create your temporary table, so something like this:

CREATE TABLE #temp (order_number INT);

(Which assumes that you have a unique id for each order number.)

Then you need to populate this table, and how you do this depends very much on how many order numbers are in your Excel workbook. You could write an SSIS package (but this would mean materialising your temporary table first), use the export wizard, etc.

One simple method is to write an Excel formula into a new column for each row in your list, then just copy this down to the bottom, copy and paste it into SSMS and execute the script you generated. You might end up with something like this as your formula:

CONCATENATE("INSERT INTO #temp SELECT ", A1, ";")

Once you have your data in a temporary table, or maybe even a physical table, you can take your queries and JOIN them to this, e.g.:

SELECT o.* FROM MyOrders o INNER JOIN #temp t ON t.order_number = o.order_number;
SELECT r.* FROM MyRefunds r INNER JOIN #temp t ON t.order_number = r.order_number;
Sign up to request clarification or add additional context in comments.

4 Comments

Hi, I have created #temp folder, now how can import data into the #temp ?
Like I said in my answer, there are multiple ways to do this. The simplest way is to manually create a script using Excel formulae. An alternative would be to do something like this: stackoverflow.com/questions/12090555/…
I have tried it ,but this is the error message - Cannot resolve the collation conflict between "Latin1_General_CI_AS" and "Latin1_General_100_CI_AS" in the equal to operation.
add "COLLATE DATABASE_DEFAULT after the JOIN condition, you will need to do this for any CHAR-type JOIN that uses a different COLLATION. Or you could get around this by enforcing exactly the right COLLATION in your temp table. So if you had "ON t.x = o.x;" you would change this to "ON t.x = o.x COLLATE DATABASE_DEFAULT;".

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.