Solved

Passing Excel values to a SQL Server 2012 view

Posted on 2014-11-15
5
187 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
ID: 40444673
0
 
LVL 13

Accepted Solution

by:
Koen Van Wielink earned 500 total points
ID: 40445315
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
ID: 40445749
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 13

Expert Comment

by:Koen Van Wielink
ID: 40445767
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
ID: 40445773
Cool..

Thanks
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
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.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

777 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