将参数从Access变量传递到SQL Server存储过程

时间:2014-01-09 18:18:08

标签: sql-server vba variables stored-procedures parameters

我正在尝试使用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);

1 个答案:

答案 0 :(得分:1)

当您尝试添加参数时,您只需将变量本身传递给调用。

'' 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 &")

问题在于您作为参数传递的2个值:"& stateNGN &""& stateDate &"。在这种情况下,您不必对变量执行任何操作,因为它们已包含要传递给存储过程的值,CreateParameter调用将为您处理它们。

所以只需将这些调用更改为:

cmd.Parameters.Append _
    cmd.CreateParameter("@SPstateNGN", adVarChar, adParamInput, 0, stateNGN)
cmd.Parameters.Append _
    cmd.CreateParameter("@SPstateDate", adDate, adParamInput, 0, stateDate)

请注意,您还可以在一个步骤中创建参数,然后在后续步骤中为其分配值,而不是上面采用的一体化方法。有关CreateParameter的更多示例可以在各种Stack Overflow线程和MSDN网站上找到。