参数化SQL查询时的空白值

时间:2019-06-18 15:20:48

标签: sql vba

我在撇号方面遇到问题,因此我想通过参数化来避免它们。这段代码运行时没有错误,但是在数据库中没有看到“测试”,而是空白。我不知道自己想念什么。

Dim conn As ADODB.Connection
'Set conn = CurrentProject.Connection
Set conn = New ADODB.Connection
conn.ConnectionString = constring
conn.Open
Dim cmd As ADODB.Command
Set cmd = New ADODB.Command
Set cmd.ActiveConnection = conn
cmd.CommandText = "declare @gmnv varchar(20), @recid varchar(15), @Value1 varchar(200) " _
               & " set nocount OFF " _
               & " EXEC Castell_Create @gmnv OUTPUT " _
               & " EXEC Castell_SetValue @gmnv, 'accountno', '" & _
                        Sheets("Quote Sheet").Range("N22").Value & "' " _
               & " EXEC Castell_SetValue @gmnv, 'filename', @Value1 " _ 
               & " EXEC Castell_SetValue @gmnv, 'user', 'MASTER' " _
               & " EXEC Castell_SetValue @gmnv, 'notes', 'Quote ZT19-01'" _
               & " EXEC Castell_SetValue @gmnv, 'ref', '" & _
                         Replace(Sheets("Quote Sheet").Range("G7").Value, "'", "''") _
                         & " - "  & _
                         Sheets("Quote Sheet").Range("B11").Value & "' " _
               & " EXEC Castell_WriteLinkedDoc @gmnv" _
               & " EXEC Castell_GetValue @gmnv, 'recid', @recid OUTPUT" _
               & " EXEC dbo.Castell_Delete @gmnv"

Dim param1 As ADODB.Parameter
Set param1 = cmd.CreateParameter("@Value1", adVarWChar, adParamInput, 8, "test")
cmd.Parameters.Append param1
cmd.Execute

2 个答案:

答案 0 :(得分:0)

通常,ADO Command对象不支持诸如@value1之类的命名参数,但支持位置参数。因此,只需将?标记放置在所需位置并按顺序绑定它们。另外,请避免所有串联,并始终使用参数:

Dim cmd As ADODB.Command

' PREPARED STATEMENT - NO DATA (THREE UNQUOTED ? MARKERS)
sql_command = "declare @gmnv varchar(20), @recid varchar(15), @Value1 varchar(200) " _
               & " set nocount OFF " _
               & " EXEC Castell_Create @gmnv OUTPUT " _
               & " EXEC Castell_SetValue @gmnv, 'accountno', ? " _
               & " EXEC Castell_SetValue @gmnv, 'filename', ? " _ 
               & " EXEC Castell_SetValue @gmnv, 'user', 'MASTER' " _
               & " EXEC Castell_SetValue @gmnv, 'notes', 'Quote ZT19-01'" _
               & " EXEC Castell_SetValue @gmnv, 'ref', ? " _
               & " EXEC Castell_WriteLinkedDoc @gmnv" _
               & " EXEC Castell_GetValue @gmnv, 'recid', @recid OUTPUT" _
               & " EXEC dbo.Castell_Delete @gmnv"

Set cmd = New ADODB.Command
With cmd
    .ActiveConnection = conn
    .CommandText = sql_command

    ' BIND PARAMS (ADJUST TYPE AND LENGTH AS NEEDED)
    .Parameters.Append .CreateParameter("prm1", adVarWChar, adParamInput, 255, _
         Sheets("Quote Sheet").Range("N22").Value
    .Parameters.Append .CreateParameter("prm2", adVarWChar, adParamInput, 8, "test")
    .Parameters.Append .CreateParameter("prm3", adVarWChar, adParamInput, 255, _
         Sheets("Quote Sheet").Range("G7").Value & " - " & _
         Sheets("Quote Sheet").Range("B11").Value

    .Execute
End With

答案 1 :(得分:0)

谢谢您的建议。我重新设计了项目,以删除文件名中的撇号以绕过此问题。