访问Sql参数化查询帮助

时间:2017-02-23 07:56:28

标签: sql ms-access

我还是访问世界的新手,我再次需要你的帮助

我在sql上调用这个存储过程

    ALTER PROCEDURE [dbo].[sp_StockMovement]
    @type   CHAR(1),
    @itmcd  VARCHAR(25),
    @descr  VARCHAR(60),
    @jsbtch VARCHAR(25),
    @poprj  VARCHAR(20),
    @spldpt VARCHAR(50),
    @frmdt  CHAR(10),
    @todt   CHAR(10)
AS
DECLARE @SQLMain VARCHAR(500), @SQLCriteria VARCHAR(450), @SQLOrder VARCHAR(50), @SQL VARCHAR(1000), 
    @count INT

SET @count = 0
SET @SQLCriteria = 'WHERE '
SET @SQLOrder = 'ORDER BY stdate, jscode, jsbatch, poproject;'

IF @itmcd LIKE '%.%' AND @itmcd NOT LIKE '%-%'
    BEGIN
        SELECT @itmcd = CONVERT(CHAR(10), prd_jscode) FROM dbo.tbl_Products WHERE prd_jscodeold = @itmcd;
    END


IF @type = 'R'
BEGIN
    SET @SQLMain = 'SELECT CASE WHEN ISNUMERIC(str_jscode) = 1 ' +
                            'THEN STUFF(STUFF(STUFF(str_jscode, 9, 0, '' ''), 6, 0, '' ''), 4, 0, '' '') ' +
                            'ELSE str_jscode END AS jscode, ' +
                        'str_description AS descr, str_jsbatch AS jsbatch, str_qty AS qty, str_unit AS unit, ' +
                        'str_ponumber AS poproject, str_supplier AS spldept, str_invoice AS invoice, ' +
                        'str_splbatch AS splbatch, CONVERT(CHAR(10), str_date, 111) AS stdate, ' +
                        'str_user AS stuser ' +
                    'FROM dbo.vw_StockReceived '


(There is more code but this is the main part so to speak)

我在访问中使用以下代码来调用stor proc

Private Sub Command3_Click()

Dim rs As ADODB.Recordset
Dim cn As ADODB.Connection
Dim cmd As ADODB.Command


Set cn = New ADODB.Connection

cn.ConnectionString = "driver={sql server};server=xxx.xxx.x.xxx;Database=JSLogistics;UID=JSLogist;PWD=JSL$35p"
cn.Open

    Set cmd = New ADODB.Command

        cmd.ActiveConnection = cn
        cmd.CommandText = "dbo.sp_StockMovement "
        cmd.CommandType = adCmdStoredProc
        cmd.NamedParameters = True

        cmd.Parameters.Append = .CreateParameter("@type", adChar, adParamInput, 1, "R")
        cmd.Parameters.Append = .CreateParameter("@itmcd", adVarChar, adParamInput, 25, "_NA_")
        cmd.Parameters.Append = .CreateParameter("@descr", adVarChar, adParamInput, 60, "_NA_")
        cmd.Parameters.Append = .CreateParameter("@jsbtch", adVarChar, adParamInput, 25, "_NA_")
        cmd.Parameters.Append = .CreateParameter("@poprj", adVarChar, adParamInput, 20, "_NA_")
        cmd.Parameters.Append = .CreateParameter("@spldpt", adVarChar, adParamInput, 50, "_NA_")
        cmd.Parameters.Append = .CreateParameter("@frmdt", adChar, adParamInput, 10, "2017/02/01")
        cmd.Parameters.Append = .CreateParameter("@todt", adChar, adParamInput, 10, "2017/02/02")

        cmd.Execute

    Set rs = New ADODB.Recordset
    With rs
        .CursorLocation = adUseClient
        .CursorType = adOpenStatic
        .LockType = adLockReadOnly
        .Open cmd
    End With
    Set Me!lstJobQuickSearch.Recordset = rs
    Me.lstJobQuickSearch.Requery

Set cmd = Nothing

End Sub

但是我得到了错误 用户定义的类型未定义

我做错了什么?

1 个答案:

答案 0 :(得分:0)

好吧,如果您保存传递查询,那么此代码应该有效:

Dim rst     As DAO.Recordset

With CurrentDb.QueryDefs("qryPassR")
   .SQL = "dbo.sp_StockMovement 'R','_NA_','_NA_','_NA_','_NA_','_NA_','2017/02/01','2017/02/01'"
   .ReturnsRecords = True
   Set rst = .OpenRecordset()
End With

尝试使用SSMS并输入:

dbo.sp_StockMovement 'R','_NA_','_NA_','_NA_','_NA_','_NA_','2017/02/01','2017/02/01'

所以从SSMS尝试这个,如果它有效,那么我的示例VBA代码将起作用(假设你在Access中保存了一个PT查询)。