将动态查询值从Excel传递到SQL

时间:2020-08-27 20:35:33

标签: sql-server excel vba

这是我的存储过程。我正在从Excel中调用StartDateEndDate作为参数。我为命令按钮执行了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

1 个答案:

答案 0 :(得分:0)

假设您希望将存储过程中的结果集转换为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 )