Solved

Passing Excel values to a SQL Server 2012 view

Posted on 2014-11-15
5
180 Views
Last Modified: 2014-11-16
I am using Excel 2010 to invoke a SQL view to populate a table into Sheet1.  The parameter values are in cells B1 and B2.

The following is the view code I am using:

SELECT     CASE WHEN AttendProviderID IS NOT NULL THEN AttendProviderID ELSE CASE WHEN PrimCareProviderID IS NOT NULL
                      THEN PrimCareProviderID ELSE CASE WHEN AdmitProviderID IS NOT NULL THEN AdmitProviderID ELSE CASE WHEN ErProviderID IS NOT NULL
                      THEN ErProviderID ELSE CASE WHEN FamilyProviderID IS NOT NULL THEN FamilyProviderID ELSE '' END END END END END AS ProvID,
                      dbo.BarVisits.VisitID, dbo.BarVisits.AccountNumber, CASE WHEN dbo.BarVisits.DischargeDateTime IS NULL
                      THEN dbo.BarVisits.ServiceDateTime ELSE dbo.BarVisits.DischargeDateTime END AS DisDt, CASE WHEN dbo.BarVisits.AdmitDateTime IS NULL
                      THEN dbo.BarVisits.ServiceDateTime ELSE dbo.BarVisits.AdmitDateTime END AS AdmDt, dbo.BarVisits.InpatientOrOutpatient,
                      dbo.BarVisits.InpatientServiceID, dbo.AbsConsultations.ConsultProviderID, dbo.AbsConsultations.ConsultSeqID
FROM         dbo.BarVisitFinancialData2 RIGHT OUTER JOIN
                      dbo.AbsConsultations RIGHT OUTER JOIN
                      dbo.BarVisits ON dbo.AbsConsultations.VisitID = dbo.BarVisits.VisitID ON dbo.BarVisitFinancialData2.VisitID = dbo.BarVisits.VisitID
WHERE     (CASE WHEN dbo.BarVisits.DischargeDateTime IS NULL THEN dbo.BarVisits.ServiceDateTime ELSE dbo.BarVisits.DischargeDateTime END BETWEEN
                     sheet1.range("B1").value AND sheet1.range("B2").value)

When the date parameters are hard coded, i.e 2014-10-01 and 2014-10-31, it works fine.


The where clause is not working.  Can someone look at it and let me know what I need to correct?
0
Comment
Question by:GPSPOW
  • 2
  • 2
5 Comments
 
LVL 42

Expert Comment

by:EugeneZ
Comment Utility
0
 
LVL 12

Accepted Solution

by:
Koen Van Wielink earned 500 total points
Comment Utility
If you're using this code purely in Excel, I'd recommend changing it to a stored procedure, or alternatively write a procedure that reads your view. The procedure can then have 2 date parameters as input which you can use in your where clause:

create procedure uSP_yourProcedureName

@dateFrom datetime
, @dateTo datetime

As

--place your select query here
where yourDate > @dateFrom and yourDate < @dateTo 

Open in new window


Note I didn't use BETWEEN for the dates where clause. PortletPaul wrote an excellent article on why BETWEEN is a bad idea.

Call this procedure from Excel and use ?'s for each parameter. There are a few working syntaxes, but I usually use this one:

{call uSP_yourProcedureCode(?, ?)}

Open in new window


Excel will now recognize the parameters and allow you to set cell references to have them filled each time the report is refreshed.
0
 

Author Closing Comment

by:GPSPOW
Comment Utility
The stored procedure works great.

However, it only works on my login.  I will need to speak with the DBA and get the users the proper access.

Thanks

Glen
0
 
LVL 12

Expert Comment

by:Koen Van Wielink
Comment Utility
Add the following line after the create statement of the procedure, before "as" but after the parameter declaration:

with execute as owner

Open in new window


This will execute the procedure with your rights rather than the person running it. It prevents you from having to give everyone rights to the database. Just make sure to secure the file to ensure non-authorized people can't execute it.
0
 

Author Comment

by:GPSPOW
Comment Utility
Cool..

Thanks
0

Featured Post

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

762 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

13 Experts available now in Live!

Get 1:1 Help Now