?
Solved

Passing Excel values to a SQL Server 2012 view

Posted on 2014-11-15
5
Medium Priority
?
201 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
[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
  • 2
5 Comments
 
LVL 43

Expert Comment

by:Eugene Z
ID: 40444673
0
 
LVL 13

Accepted Solution

by:
Koen Van Wielink earned 2000 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

Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
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 will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

771 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