这里是我需要将访问功能翻译成下面的SQL都是我得不到同样的结果我错过了什么?当我在桌子上运行它时,会有轻微的差异。访问代码是正确的,我只需要确保我的SQL代码与访问相同。
谢谢!
- 访问VBA代码:
Public Function DelDuplicates()
'save the deleted double counts
DoCmd.OpenQuery "QrySavetrash"
Dim Mysqlfin, MysqlTrd As String
Dim Myrecordfin As ADODB.Recordset
Dim MyrecordTrd As ADODB.Recordset
Mysqlfin = "select * from TBL_TEMP1"
MysqlTrd = "select * from TBL_TEMP2"
Set Myrecordfin = New ADODB.Recordset
Myrecordfin.ActiveConnection = CurrentProject.Connection
Myrecordfin.CursorType = adOpenDynamic
Myrecordfin.LockType = adLockOptimistic
Myrecordfin.Open Mysqlfin
Set MyrecordTrd = New ADODB.Recordset
MyrecordTrd.ActiveConnection = CurrentProject.Connection
MyrecordTrd.CursorType = adOpenDynamic
MyrecordTrd.LockType = adLockOptimistic
MyrecordTrd.Open MysqlTrd
If Not MyrecordTrd.EOF Then
MyrecordTrd.MoveFirst
End If
If Not Myrecordfin.EOF Then
Myrecordfin.MoveFirst
End If
If MyrecordTrd.EOF Then
MsgBox " Table TBL_TEMP2 is empty, please check"
End If
If Myrecordfin.EOF Then
MsgBox "Table TBL_TEMP1 is empty, please check to make the preprocessing meaningful"
End If
RowNo = 0
Do While Not MyrecordTrd.EOF
Do While Not Myrecordfin.EOF
If MyrecordTrd!Invoice = Myrecordfin!INV_NO Then
If IsNull(MyrecordTrd!Ck_No) Then MyrecordTrd!Ck_No = " "
If IsNull(Myrecordfin!Ck_No) Then Myrecordfin!Ck_No = " "
If (MyrecordTrd!Ck_No = Myrecordfin!Ck_No) Or (Nz(MyrecordTrd!Ck_No) = Nz(Myrecordfin!Ck_No)) Then
MyrecordTrd.Delete
RowNo = RowNo + 1
Exit Do
End If
End If
If Not Myrecordfin.EOF Then
Myrecordfin.MoveNext
End If
Loop
Myrecordfin.MoveFirst
If Not MyrecordTrd.EOF Then
MyrecordTrd.MoveNext
End If
Loop
MyrecordTrd.Close
Myrecordfin.Close
'dbinfo.Close
Set MyrecordTrd = Nothing
Set Myrecordfin = Nothing
MsgBox " preprocessing is done " & RowNo & " duplicate records have been deleted"
'Set dbinfo = Nothing
End FunctionPublic Function DelDuplicates()
'save the deleted double counts
DoCmd.OpenQuery "QrySavetrash"
--- SQL 2012 CODE
UPDATE [dbo].[TBL_TEMP1] SET [CK_NO] = LTRIM(RTRIM([CK_NO]))
UPDATE [dbo].[TBL_TEMP2] SET [CK_NO] = LTRIM(RTRIM([CK_NO]))
UPDATE [dbo].[TBL_TEMP1] SET [INV_NO] = LTRIM(RTRIM([INV_NO]))
UPDATE [dbo].[TBL_TEMP2] SET [Invoice] = LTRIM(RTRIM([Invoice])
UPDATE [dbo].[TBL_TEMP1] SET [CK_NO] = ' ' WHERE ISNULL([CK_NO], '0') = '0'
UPDATE [dbo].[TBL_TEMP2] SET [CK_NO] = ' ' WHERE ISNULL([CK_NO], '0') = '0'
UPDATE [dbo].[TBL_TEMP1] SET [INV_NO] = ' ' WHERE ISNULL([INV_NO], '0') = '0'
UPDATE [dbo].[TBL_TEMP2] SET [Invoice] = ' ' WHERE ISNULL( [Invoice], '0') = '0'
DELETE FROM [dbo].[TBL_TEMP2] FROM [dbo].[TBL_TEMP2]
LEFT JOIN [dbo].[TBL_TEMP1] ON [dbo].[TBL_TEMP2].[Invoice] = [TBL_TEMP1].[INV_NO]
WHERE [dbo].[TBL_TEMP2].[Ck_No] = [TBL_TEMP1].[CK_NO]
DELETE FROM [dbo].[TBL_TEMP2] FROM [dbo].[TBL_TEMP1]
LEFT JOIN [dbo].[TBL_TEMP2] ON [TBL_TEMP1].[INV_NO] = [dbo].[TBL_TEMP2].[Invoice]
WHERE [TBL_TEMP1].[CK_NO] = [dbo].[TBL_TEMP2].[Ck_No]
答案 0 :(得分:0)
你的SQL应该看起来像这样做VBA正在做的事情。您似乎正在尝试删除带有匹配发票的tbl_temp2记录,并检查来自tbl_temp1的#(或空检查#)。
delete a
from tbl_temp2 a
inner join tbl_temp1 b
on a.invoice = b.inv_no
and case when a.ck_no is null then -1 else a.ck_no end
= case when b.ck_no is null then -1 else b.ck_no end ;
示例:http://sqlfiddle.com/#!3/20417/1
我获取了您的VBA代码并在MS Access 2013中创建了相同的数据,并针对它运行了VBA代码。结果与SQL的结果相同。
该声明根据发票编号和支票编号匹配两个表中的记录。如果找到匹配项,则会从tbl_temp2
中删除该记录。对于空校验数字,动态使用-1。
VBA也匹配发票号和支票号。此外,如果检查号为空,则将其转换为空字符串(就像我使用了-1)并匹配。