Passing Excel values to a SQL Server 2012 view

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:

                      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?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

EugeneZ ZhitomirskySQL SERVER EXPERTCommented:
Koen Van WielinkBusiness Intelligence SpecialistCommented:
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


--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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GPSPOWAuthor Commented:
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.


Koen Van WielinkBusiness Intelligence SpecialistCommented:
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.
GPSPOWAuthor Commented:

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.