【发布时间】:2020-08-27 20:35:33
【问题描述】:
这是我的存储过程。我正在调用StartDate、EndDate 作为 Excel 中的参数。我为命令按钮执行了 VBA。问题是我必须通过StartDate 和EndDate 才能获取信息。有什么方法可以从StartDate 或EndDate 提取特定日期的数据吗?有时我从两个参数中提取数据?不知道逻辑是如何工作的。
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
【问题讨论】:
-
你能澄清你的问题吗?我不明白这个问题。传递
StartDate和EndDate值有什么问题。为什么你不能在你需要的任何特定日期范围内通过? -
想查看特定日期或特定日期之间的数据 ...存储的过程不就是
WHERE条件:WHERE SellStartDate >= @SellStartDate AND SellEndDate <= @SellEndDate所做的吗?也许向我们展示当前结果和期望结果的数据。
标签: sql-server excel vba