简体   繁体   English

在Excel / VBA中更改存储过程参数

[英]Changing Stored Procedure Parameters in Excel/VBA

I'm successfully pulling data into excel via SQL queries formatted like so in VBA: 我已经通过VBA中这样格式化的SQL查询成功将数据提取到excel中:

.Open "exec sp_MyProcedure @Node_Id = 05,@Subsidiary_Cd = '1',@WeekEndDate = '2016-05-28', @JobType = '12',@ReportLevel = 4"

I would like to either have the user enter the most current week ending date, which would change the @WeekEndDate parameter to their entry or have a cell displaying the most recent week end date without needing user entry (if that's possible?. All other parameters remain the same. I'm new to vba and don't have much experience in excel to any help is appreciated. 我想让用户输入最新的周结束日期,这会将@WeekEndDate参数更改为他们的输入,或者让一个单元格显示最近的周结束日期而无需用户输入(如果可能的话。)所有其他参数我是vba的新手,没有很多excel方面的经验,无法获得任何帮助。

**Can edit question if more data is needed **如果需要更多数据,可以编辑问题

Consider this: 考虑一下:

Dim NodeId, SubsidiaryCd, WeekEndDate, JobType, ReportLevel

NodeId = 05
SubsidiaryCd = "1"
WeekEndDate = "2016-05-28"
JobType = "12"
ReportLevel = 4

.Open "exec sp_MyProcedure " & _
    "@Node_Id = " & NodeId & "," & _
    "@Subsidiary_Cd = '" & SubsidiaryCd & "'," & _
    "@WeekEndDate = '" & WeekEndDate & "', " & _
    "@JobType = '" & JobType & "'," & _
    "@ReportLevel = " & ReportLevel

This is exactly the same as the previous instruction; 这与上一条指令完全相同; you now just need to change the constant value for the WeekEndDate variable to the desired date, like: 您现在只需将WeekEndDate变量的常量值更改为所需的日期,例如:

WeekEndDate = Format(Date(), "yyyy-mm-dd")

First, it depends on what you consider the last day of a given week. 首先,这取决于您认为给定一周的最后一天。 For my test, I'm assuming Saturday. 为了考试,我假设星期六。

Second, I'm assuming by "most current week", you mean the last FULL week (as in not the week we are currently in). 其次,我假设“最近的一周”是指最后的完整周(因为不是我们当前所在的周)。

Third, I'm assuming the code snippet you included is the SQL you have integrated within your VBA code. 第三,假设您包含的代码段是已集成到VBA代码中的SQL。

So, if you have =TODAY() in Cell A1, you can put the following formula in Cell A2, then change your VBA snippet to the following: 因此,如果单元格A1中有=TODAY() ,则可以在单元格A2中添加以下公式,然后将VBA代码段更改为以下内容:

Formula: =A1-WEEKDAY(A1) 公式: =A1-WEEKDAY(A1)

.Open "exec sp_MyProcedure @Node_Id = 05,@Subsidiary_Cd = '1',@WeekEndDate = '" & ActiveSheet.Cells(2, 1).Value & "', @JobType = '12',@ReportLevel = 4"

From here, you could follow the same idea to customize all parameters in your procedure. 从这里开始,您可以遵循相同的想法来自定义过程中的所有参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM