Solved

Query Database Using SQL statement and bring to Excel

Posted on 2014-09-05
4
251 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
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.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

773 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