Solved

Query Database Using SQL statement and bring to Excel

Posted on 2014-09-05
4
250 Views
Last Modified: 2014-09-19
Hi Anyone,
I need to bring into Excel 2013 a daily Trial Balance from a SQL database of financial figures.
I have the SQL statement;
DECLARE @ReportDate DateTime
SELECT TOP 1 @ReportDate = HotelDate From tblHotels
SET @ReportDate = @ReportDate + {Offset:number}
DECLARE @ReportDatePlusOneDay DateTime
DECLARE @PostingType_Sale Int
DECLARE @PostingType_VAT Int
SET @ReportDatePlusOneDay=@ReportDate+1;
SET @PostingType_Sale=1;
SET @PostingType_VAT=3;
SELECT
      tblAccounts.Code,
      tblAccounts.Description,
      GrossAmount = (
            SELECT ISNULL(SUM(tblPostings.Credit - tblPostings.Debit),0)
            FROM tblPostings
            INNER JOIN tblJournals jtev ON jtev.JournalID = tblPostings.JournalID
            WHERE (tblPostings.AccountID = tblAccounts.AccountID)
            AND (tblPostings.PostingType <> @PostingType_VAT)
            AND (jtev.JournalDate >= @ReportDate)
            AND (jtev.JournalDate < @ReportDatePlusOneDay))
      -- VAT
      + (
            SELECT ISNULL(SUM(tblPostings.Credit - tblPostings.Debit),0)
            FROM tblPostings
            INNER JOIN tblJournals jtiv ON jtiv.JournalID = tblPostings.JournalID
            WHERE EXISTS (SELECT *
                                FROM tblPostings
                                WHERE (tblPostings.AccountID = tblAccounts.AccountID)
                                AND (jtiv.JournalID = tblPostings.JournalID)
                                AND (tblPostings.PostingType = @PostingType_Sale))
            AND (tblPostings.PostingType = @PostingType_VAT)
            AND (jtiv.JournalDate >= @ReportDate)
            AND (jtiv.JournalDate < @ReportDatePlusOneDay))
FROM tblAccounts
ORDER BY tblAccounts.Code

BUT - I need to base the whole query on an automated date in a cell in Excel and not the date at the top of tblHotels

Therefore the question is, how do I set the parameter in this instance?

Thanks,
Andrew
0
Comment
Question by:Andrew Prior
  • 2
4 Comments
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40305667
Here's an article on how to hotwire Excel into SQL Server to pass cell-based parameters into a SQL Stored Procedure, and return the result set back to Excel, complete with a whole lot of images to show how it's done, and the doc containing source code at the end.
0
 
LVL 10

Accepted Solution

by:
Ray earned 500 total points
ID: 40306324
" & Range("D1").Value & "

Change the cell value from "D1" to whatever cell you want the value from.  For example if D1 contained the value: TEST

then
SQL =  " from " & Range("D1").Value & ".TableName "

would be passed to sql as
from TEST.TableName
0
 

Author Closing Comment

by:Andrew Prior
ID: 40333717
Really need the date format converted from Excel date
0
 
LVL 10

Expert Comment

by:Ray
ID: 40333895
Converted how? From what to what?  It should pass to sql in the same format as the cell it comes from. So likely you can just change the cell format and get what you need.
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now