使用ADODB对象在Excel中从VBA调用存储过程时,似乎会获得意外的空白区域。从SQL Server Management Studio运行该过程时不会发生此问题。
exec usp_testloginalert '2015-11-29 00:00:00','2015-11-30 00:00:00',N'screen'
更新
来自vba的代码
Function querylogs(ByVal todate As Date, ByVal fromdate As Date, ByVal desc As String)
Dim cn As ADODB.Connection
Dim cmdProd As ADODB.command
Dim prs As New ADODB.Recordset
sQuery = "usp_testloginalert"
Set cn = New ADODB.Connection
With cn
.Provider = "SQLOLEDB"
sConnection = "dbconn"
.ConnectionString = sConnection
.ConnectionTimeout = 1000
.CommandTimeout = 1000
End With
cn.Open sConnection
Set cmdProd = New ADODB.command
With cmdProd
.ActiveConnection = cn
.CommandType = adCmdStoredProc
.CommandText = "usp_testloginalert"
.CommandTimeout = 1000
End With
Dim prmFromDate As ADODB.Parameter
Set prmFromDate = cmdProd.CreateParameter("@FromDate", adDBDate,adParamInput)
prmFromDate.Value = fromdate
cmdProd.Parameters.Append prmFromDate
Dim prmToDate As ADODB.Parameter
Set prmToDate = cmdProd.CreateParameter("@ToDate", adDBDate, adParamInput)
prmToDate.Value = todate
cmdProd.Parameters.Append prmToDate
Dim prmDescritpion As ADODB.Parameter
cmdProd.Parameters.Append cmdProd.CreateParameter("Descritpion",adBSTR,adParamInput, 100, desc)
Set prs = New Recordset
prs.CursorLocation = adUseClient
prs.Open cmdProd.Execute
Application.EnableCancelKey = True
cn.Close
Set querylogs = prs
End Function
答案 0 :(得分:0)
如果我们看不到您的程序代码,很难回答,但我建议您使用LTRIM和RTRIM删除前导/尾随空白。
以下内容:LTRIM(RTRIM( /* character_xpression */ ))
<强>更新强>
如果它不是SQL问题,请尝试检查相关帖子:
Remove leading or trailing spaces in an entire column of data
Delete blanks from start and end of a string in vba
http://www.excelitems.com/2009/03/remove-extra-spaces-from-cell-value.html
答案 1 :(得分:0)
问题已解决,请将参数长度设置为传递的变量的长度。
Dim lendesc As Integer
lendesc = Len(desc) + 1
Dim prmDescritpion As ADODB.Parameter
cmdProd.Parameters.Append cmdProd.CreateParameter("Descritpion", adBSTR,adParamInput, lendesc, desc)