我有以下(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数据类型转换为datetime数据类型 超出范围的日期时间值。'
存储过程在SQL Server Management Studio中完美运行,但在Microsoft Access中引发错误。我真的很感激,如果有人能找出造成问题的原因..
答案 0 :(得分:0)
以另一种方式解决问题:
strSQL = "INSERT INTO dbo.wiegen ([Tag], [Aufträge_anzahl], [Wiegeraum])
SELECT left([BUCHUNG_BIS],11) as [Tag], count(distinct [AUFTRAGSNUMMER]) as [Aufträge_anzahl], Kurztext as [Wiegeraum]
from dbo.tblZEITERFASSUNG
inner join
dbo.tblBELEGUNGSEINHEIT on tblZEITERFASSUNG.ID_BELEGUNGSEINHEIT=tblBELEGUNGSEINHEIT.ID
where ID_BUCHUNGSART=9 and ID_BELEGUNGSEINHEIT
in
(SELECT ID_BELEGUNGSEINHEIT FROM dbo.tblPROZESS_BELEGUNGSEINHEIT WHERE ID_PROZESS = 3)
and [ABSCHLUSS]=1 AND left([BUCHUNG_BIS],11)= left(GETDATE(), 11)
group by left([BUCHUNG_BIS],11), [Kurztext]"
用sql内置的left()函数替换了我的datepart2函数,现在它成功执行,没有任何错误。