Solved

Query Database Using SQL statement and bring to Excel

Posted on 2014-09-05
4
248 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
Comment Utility
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
Comment Utility
" & 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
Comment Utility
Really need the date format converted from Excel date
0
 
LVL 10

Expert Comment

by:Ray
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
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.

763 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

10 Experts available now in Live!

Get 1:1 Help Now