【问题标题】:Passing Dynamic Query Values from Excel to SQL将动态查询值从 Excel 传递到 SQL
【发布时间】:2020-08-27 20:35:33
【问题描述】:

这是我的存储过程。我正在调用StartDateEndDate 作为 Excel 中的参数。我为命令按钮执行了 VBA。问题是我必须通过StartDateEndDate 才能获取信息。有什么方法可以从StartDateEndDate 提取特定日期的数据吗?有时我从两个参数中提取数据?不知道逻辑是如何工作的。

CREATE PROCEDURE dbo.ProductListPrice @SellStartDate as Date, @SellEndDate as Date 
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
   SET NOCOUNT ON;

-- Insert statements for procedure here
SELECT
    PR.[Name] ProductName
       ,PS.Name SubCategory
       ,PC.NAME ProductCategory
       ,PM.Name ProductModel
       ,[StandardCost]
       ,[ListPrice]   
       ,CAST([SellStartDate] AS DATE) SellStartDate
       ,CAST([SellEndDate] AS DATE) SellEndDate
FROM [AdventureWorks2014].[Production].[Product] PR
INNER JOIN [AdventureWorks2014].[Production].[ProductSubcategory] PS 
ON PR.[ProductSubcategoryID]=PS.[ProductSubcategoryID] 
INNER JOIN [AdventureWorks2014].[Production].[ProductCategory] PC 
ON PS.ProductCategoryID=PC.ProductCategoryID 
INNER JOIN [AdventureWorks2014].[Production].[ProductModel] PM 
ON PR.ProductModelID=PM.ProductModelID
WHERE SellStartDate>=@SellStartDate AND SellEndDate<=@SellEndDate
ORDER BY SellStartDate,productname

END

VBA

Private Sub CommandButton1_Click()

Dim SellStartDate As Date  'Declare the SellStartDate as Date
Dim SellEndDate As Date    'Declare the SellEndDate as Date

SellStartDate = Sheets("Sheet1").Range("B3").Value   'Pass value from cell B3 to SellStartDate variable
SellEndDate = Sheets("Sheet1").Range("B4").Value     'Pass value from cell B4 to SellEndDate variable

'Pass the Parameters values to the Stored Procedure used in the Data Connection
With ActiveWorkbook.Connections("AdventureWorksConnection").OLEDBConnection
.CommandText = "EXEC dbo.ProductListPrice '" & SellStartDate & "','" & SellEndDate & "'"
ActiveWorkbook.Connections("AdventureWorksConnection").Refresh
    
End With
End Sub

【问题讨论】:

  • 你能澄清你的问题吗?我不明白这个问题。传递 StartDateEndDate 值有什么问题。为什么你不能在你需要的任何特定日期范围内通过?
  • 想查看特定日期或特定日期之间的数据 ...存储的过程不就是 WHERE 条件:WHERE SellStartDate &gt;= @SellStartDate AND SellEndDate &lt;= @SellEndDate 所做的吗?也许向我们展示当前结果和期望结果的数据。

标签: sql-server excel vba


【解决方案1】:

假设您希望将存储过程中的结果集转换为 excel,您可以在 VBA 模块中使用类似的内容。

将日期放在电子表格的几个单元格中,假设您在单元格 A1 和 A2 中有日期

在您的点击按钮上使用以下 VBA:(这是我使用的,您可以修改它以适应)

Sub your_sub_name()

Dim con As ADODB.Connection
Dim cmd As ADODB.Command
Dim rs As ADODB.Recordset
Dim par As String
Dim WSP1 As Worksheet
Set con = New ADODB.Connection
Set cmd = New ADODB.Command
Set rs = New ADODB.Recordset



Application.DisplayStatusBar = True
Application.StatusBar = "Contacting SQL Server..."

' Remove any values in the cells where we want to put our Stored Procedure's results.
Dim rngRange As Range
Set rngRange = Range("A3:H299")
ActiveSheet.Range("A3:H299").ClearContents
ActiveSheet.Select
Range("A3:H299").Select
Selection.ClearContents

' Log into our SQL Server, and run the Stored Procedure
con.Open "Provider=SQLOLEDB;Data Source=your_server_name;Initial Catalog=your_database_name;Integrated Security=SSPI;Trusted_Connection=Yes;"
cmd.ActiveConnection = con

Dim prmstation As ADODB.Parameter
Dim prmDate As ADODB.Parameter
Dim prmShift As ADODB.Parameter

' Set up the parameter for our Stored Procedure
' (Parameter types can be adVarChar,adDate,adInteger)
cmd.Parameters.Append cmd.CreateParameter("SellStartDate", adDate, adParamInput, 10, Range("A1").Text)
cmd.Parameters.Append cmd.CreateParameter("SellEndDate", adDate, adParamInput, 10, Range("A2").Text)


Application.StatusBar = "Running stored procedure..."
cmd.CommandText = "ProductListPrice "
Set rs = cmd.Execute(, , adCmdStoredProc)

' Copy the results to cell B7 on the first Worksheet
Set WSP1 = ActiveSheet
WSP1.Activate
If rs.EOF = False Then WSP1.Cells(3, 1).CopyFromRecordset rs

rs.Close
Set rs = Nothing
Set cmd = Nothing

con.Close
Set con = Nothing

Application.StatusBar = "Data successfully updated for : " & Range("AS36").Text
End Sub

您还需要像这样修改 WHERE 子句

where
( @SellStartDate <>'' and @SellEndDate <>'' and   SellStartDate>=@SellStartDate AND SellEndDate<=@SellEndDate )
or

(@SellStartDate <>''  and @SellEndDate ='' and  SellStartDate>=@SellStartDate)

or

(@SellStartDate =''  and @SellEndDate <>'' and   SellEndDate<=@SellEndDate )

【讨论】:

  • 我想要调用一两个参数的能力。
  • 相应地修改您的存储过程。现在你必须输入两个参数
  • 我想选择输入一个或两个。如果想在同一进程中查看特定日期或特定日期之间的数据
猜你喜欢
  • 2018-07-26
  • 1970-01-01
  • 1970-01-01
  • 2014-05-15
  • 1970-01-01
  • 2016-04-22
  • 1970-01-01
  • 1970-01-01
  • 2018-05-12
相关资源
最近更新 更多