define parameters from an Excel Cell for a CASE WHEN in SQL

后端 未结 1 1165
终归单人心
终归单人心 2020-12-22 03:12

I want to be able to define my parameter SA.FiscalWeek in the below code from a cell in Excel

SUM(CASE WHEN SA.FISCALYEAR = 2017 
AND SA.FISCALWEEK = 19 THEN         


        
相关标签:
1条回答
  • 2020-12-22 03:32

    You can use vba to pass the query to the command text, Use a helper sheet with your query listed line by line

    =" line 1 of query"

    =" line 2 etc. "

    =" line 3&E4&"rest of line etc. "

    E4 would be a cell ref to make the query dynamic from Excel then concatenate these lines into one cell,

    use vba similiar to the below, my range "SQLCOUNTRY" is the cell of concatenated query

     With ActiveWorkbook.Connections("EUUKSQL01 dashboard StockSalesAggregateWeek2") _
            .OLEDBConnection
        .CommandText = Sheets("CountryScript").Range("SQLCOUNTRY").Value
        End With
    

    This way any part of the query can be driven off an existing excel Cell

    0 讨论(0)
提交回复
热议问题