Solved

Query Database Using SQL statement and bring to Excel

Posted on 2014-09-05
4
253 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 66

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 11

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 11

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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

717 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