Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 将动态查询值从Excel传递到SQL_Sql Server_Excel_Vba - Fatal编程技术网

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

Sql server 将动态查询值从Excel传递到SQL,sql-server,excel,vba,Sql Server,Excel,Vba,这是我的存储过程。我正在调用StartDate,EndDate作为Excel中的参数。我为命令按钮执行了VBA。问题是我必须通过StartDate和EndDate才能获得信息。是否有任何方法可以从StartDate或EndDate提取特定日期的数据?有时我从两个参数中提取数据?不知道逻辑是如何运作的 创建过程dbo.ProductListPrice@SellStartDate作为日期,@SellEndDate作为日期 像 开始 -已添加SET NOCOUNT以防止额外的结果集丢失 不计数; -在

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

创建过程dbo.ProductListPrice@SellStartDate作为日期,@SellEndDate作为日期 像 开始 -已添加SET NOCOUNT以防止额外的结果集丢失 不计数; -在此处插入过程的语句 选择 PR.[Name]ProductName ,PS.Name子类别 ,PC.NAME ProductCategory ,PM.Name ProductModel ,[标准成本] ,[价目表] ,将[SellStartDate]转换为日期SellStartDate ,将[SellEndDate]转换为日期SellEndDate 自[AdventureWorks2014]【生产】【产品】请购单 内部联接[AdventureWorks2014].[Production].[ProductSubcategory]PS 在PR.[ProductSubcategoryID]=PS.[ProductSubcategoryID] 内部连接[AdventureWorks2014].[Production].[ProductCategory]PC 在PS.ProductCategoryID=PC.ProductCategoryID上 内部连接[AdventureWorks2014].[Production].[ProductModel]PM 在PR.ProductModelID=PM.ProductModelID上
其中SellStartDate>=@SellStartDate和SellEndDate假设您希望将存储过程中的结果集输入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 )

我希望能够调用一个或两个参数。相应地修改存储过程。按照现在的方式,您必须同时输入两个参数。我希望可以选择输入一个或两个参数。如果您想在同一时间内查看特定日期或特定日期之间的数据,请澄清您的问题?我不明白这个问题。传递StartDate和EndDate值有什么问题。为什么不能在需要的特定日期范围内传递数据?希望查看特定日期或特定日期之间的数据。。。这不是存储过程对WHERE条件所做的吗:WHERE SellStartDate>=@SellStartDate和SellEndDate