【发布时间】:2014-01-09 18:18:07
【问题描述】:
我正在尝试使用 ADODB 连接将变量/参数从 Access 表传递到 SQL Server 中的存储过程。我在 Access from 和 Access 查询中提取变量(所以我试图传递一个每次执行代码时都会更新的变量)。尝试在 Access VBA 代码中传递以下变量时,我收到以下错误消息:
参数对象定义不正确。提供的信息不一致或不完整。
我是这种编码的新手。有人可以就如何将变量(Access 查询的结果)传递给存储过程,然后在存储过程中调用这些变量提供一些指导吗?
访问 VBA 代码:
'''CREATE LINKED TABLE THAT PULLS FROM TBL_STATEMENT_MASTER IN SQL
''variables to pass to get dFileRE
'' (1) stateDate
stateDate = getItem("select distinct paydate from update_statement_master_re")
'' (2) stateNGN
stateNGN = getItem("select distinct deal_code from update_statement_master_re")
user = GetUser()
'' Connect to Data Source - Securities DB - SQL Server
Set dbconn = New ADODB.Connection
dbconn.ConnectionString = "driver=SQL Server;server=R7SQL1;database=SecuritiesDB;trusted_connection=YES"
dbconn.Open dbconn.ConnectionString
Set cmd = New ADODB.Command
cmd.ActiveConnection = dbconn
'' Set CommandText equal to the stored procedure name (spStatementCheck)
cmd.CommandType = adCmdStoredProc
cmd.CommandText = "spStatementCheck"
cmd.NamedParameters = True 'paramStatementCheck'
'' THIS IS THE PART OF THE CODE THAT IS CRASHING – I AM NOT SURE HOW TO CALL THESE VARIABLES
cmd.Parameters.Append _
cmd.CreateParameter("@SPstateNGN", adVarChar, adParamInput, 0, "& stateNGN &")
cmd.Parameters.Append _
cmd.CreateParameter("@SPstateDate", adDate, adParamInput, 0, "& stateDate &")
SQL Server 存储过程代码:
USE [SecuritiesDB_TEST]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[spStatementCheck]
@SPstateNGN as nvarchar(25),
@SPstateDate as datetime
AS
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
INSERT INTO UPDATE_Statement_Master_Check ( NGN_Full, [Date], [DESCRIPTION], AMOUNT, NGN_SHORT, Series, FileDate, EffectiveDate, ActualDate, [Source], ID )
SELECT tbl_Statement_Master.NGN_Full, tbl_Statement_Master.[Date],
tbl_Statement_Master.[DESCRIPTION],
tbl_Statement_Master.AMOUNT,
tbl_Statement_Master.NGN_SHORT,
tbl_Statement_Master.Series,
tbl_Statement_Master.FileDate,
tbl_Statement_Master.EffectiveDate,
tbl_Statement_Master.ActualDate,
tbl_Statement_Master.[Source],
tbl_Statement_Master.ID
FROM tbl_Statement_Master
where tbl_Statement_Master.[Date] <> @SPstateDate
and tbl_Statement_Master.NGN_Full = @SPstateNGN
and tbl_statement_master.FileDate = (Select distinct max(filedate) from tbl_statement_master
where [DATE] = @SPstateDate and NGN_Full = @SPstateNGN);
【问题讨论】:
-
我可能在这里遗漏了一些东西,但
GetItem行会返回多个值,而您正试图将这些多个值传递给CreateParameter调用。您是否希望在某处有一个循环来循环遍历每条记录并根据该记录中的参数值执行存储过程。我也不明白创建参数的"& stateNGN &"部分。过去,我只是将变量放在该参数中,不带引号或与号。 -
GetItem 只会返回一个值。我使用了 stateNGN 和 stateDate,因为每次执行代码时都需要更新这些变量,而不是对变量进行硬编码。
-
点在
GetItem,第二点是关于CreateParameter("@SPstateNGN", adVarChar, adParamInput, 0, stateNGN),只是删除引号和&符号,因为变量中保存的任何值都作为参数值传入。 -
太好了,谢谢你,这似乎奏效了。存储过程代码看起来正确吗?我不确定参数是否会真正传递给存储过程。有没有办法检查它们是否被正确传递?
-
非常感谢!我能够通过创建临时表并运行两个查询来测试并确保变量正在传递。
标签: sql-server vba variables stored-procedures parameters