Link to home
Start Free TrialLog in
Avatar of GPSPOW
GPSPOWFlag for United States of America

asked on

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:

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?
Avatar of EugeneZ
EugeneZ
Flag of United States of America image

ASKER CERTIFIED SOLUTION
Avatar of Koen Van Wielink
Koen Van Wielink
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of GPSPOW

ASKER

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
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.
Avatar of GPSPOW

ASKER

Cool..

Thanks