【问题标题】:Is there a way to create an Excel query parameter for a variable?有没有办法为变量创建 Excel 查询参数?
【发布时间】:2021-06-16 17:04:33
【问题描述】:

我正在使用 Query 将一些数据带入 Excel,但是行数过多,文件超过 70 MB,崩溃了几次。 我需要按月恢复的值,其中有人在 Excel 单元格中填写月份,Excel 查询会使用该值更新。

我尝试做某事,但没有得到我预期的结果 我在下面尝试的查询:

DECLARE @Month Varchar(2);

SET @Month = ?

select distinct C.Code as ResourceCode, C.Name as ResourceName, sum( case when B.Code = '069' then MovQty * -1 else MovQty end) QTY, 
B.Code as MovCode, B.Name as MovType from TBLMovEv A

inner join TBLMovType B on A.IDMovType = B.IDMovType

inner join TBLResource C on C.IDResource = a.IDResource

inner join TBLProduct D on D.IDProduct = A.IDProduct where

year(DtMov) = 2021

and Month(DtMov) = @Month

and day(DtMov) < (case when Month(getdate()) <> @Month then day(getdate()) else 32 end)

and B.Code in ('068', '069')

and C.Code NOT like '[CKLOM]%'

and C.Code not like '[ABQ][E][M]%'

and MovQty <> 0

GROUP BY B.CODE, B.NAME, C.CODE, C.NAME, DtMov

ORDER BY C.Code

我的问题是,当参数化的月份与 getdate 月份不同时,我希望那一天可能是

我想知道是否有一种方法可以将@Month 变量设置为参数,或者一种解决方法。

我还尝试获取仅包含 de month 的列,然后通过 excel 公式进行过滤,但它仍然得到接近 10.000 行的内容,这是我想避免的。 我可以直接从 SQL 获得的数据量越少越好。

【问题讨论】:

    标签: sql-server excel export-to-excel


    【解决方案1】:

    Power Query for Excel 用于更大的数据集。它将数据存储在压缩的列式存储中,而不仅仅是写入工作表。

    【讨论】:

    • 我试图寻找一种 Power Query 可以按照我需要的方式帮助我的方法,但找不到适合我希望 Excel 做的事情,但无论如何感谢!
    【解决方案2】:

    我通过使用 VBA 代码在连接命令文本中编写查询解决了这个问题,如下所示:

     Private Sub CommandButton1_Click()
    
    Dim X As Long
    
    X = Sheets("Sheet1").Range("A2").Value
    
    'Pass the Parameters values to the Stored Procedure used in the Data Connection
    With ThisWorkbook.Connections("ConnectionName").ODBCConnection
        .CommandText = "select * from TBLexample where Month = " & X 
        ThisWorkbook.Connections("ConnectionName").ODBCConnection.Refresh
        End With
        
    End Sub
    

    我刚刚在工作表中放置了一个命令按钮,因此当单元格中填充了所需的月份时,用户只需按下按钮,表格就会刷新。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-20
      • 2017-02-12
      • 1970-01-01
      • 1970-01-01
      • 2011-11-26
      • 2012-03-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多