我有以下 (dbo.DatePart2()) SQL 函数:
ALTER function [dbo].[DatePart2] (@date datetime)
returns datetime
as
begin
return(cast(CONVERT(varchar(12), @date, 101) AS datetime))
end
我需要使用 VBA 从 Microsoft Access 调用此函数,我尝试了以下操作:
Function InsertWiegen()
Dim conn As ADODB.Connection
Dim rcs As ADODB.Recordset
Dim strSQL As String
Dim strDel As String
Dim temp As String
Dim y As Date
Set conn = CurrentProject.Connection
temp = Format(Now(), "MM.dd.yyyy")
y = CStr(temp)
'strSQL = "INSERT INTO dbo.test ([a],[b]) SELECT * FROM dbo.test2 WHERE c = '" & y & "'"
'strSQL = "INSERT INTO dbo.test ([a], [b]) VALUES ('" & y & "', 3)"
strSQL = "select dbo.DatePart2('" & temp & "')"
Debug.Print temp
Set rcs = conn.Execute(strSQL)
Set rcs = Nothing
conn.Close
Exit Function
End Function
问题:它抛出一个错误:
“将 char 数据类型转换为日期时间数据类型导致日期时间值超出范围。”
存储过程在 SQL Server Management Studio 中运行良好,但在 Microsoft Access 中引发错误。如果有人能找出导致问题的原因,我将不胜感激。