从多个Access表中删除记录

时间:2016-03-08 15:31:16

标签: sql ms-access ms-access-2010

我有多个相同格式的表从Excel电子表格导入Access。导入会带来我想要删除的空记录。我可以使用from pyhive import hive conn = hive.Connection(host="host_running_hs2_service", port=10000, username="awoolford") cursor = conn.cursor() cursor.execute("SHOW TABLES") for table in cursor.fetchall(): print table

从一个表中删除

但是,如果我尝试使用

编写它来处理第二个表格
DELETE FROM HTS_01 WHERE TESTS Is Null;

然后我收到错误“SQL语句结束后找到的字符。”

如果我从第一行删除分号,我得到一个语法错误“查询表达式中的语法错误(缺少运算符)TESTS Null DELETE FROM HTS_030  在哪里测试是空的;“

问题是我有19张桌子。我想我可以编写19个查询,然后编写一小段代码来逐个执行查询,但我试图避免这种情况。

1 个答案:

答案 0 :(得分:0)

一位同事想出了以下内容,并且效果很好。谢谢你的帮助!

Sub delete_empty_rows()
' **************************************************************************
' J.K. DeHart
' 3/8/16
' This script will loop through all active tables in the current database and
' remove rows there the defined colmn has 'NULL' data cells
' **************************************************************************

DoCmd.SetWarnings False ' Turn warnings 'Off' for DELETE function

Dim db As Database
Dim tbl As TableDef
Dim fieldName
Dim sqlString As String
Set db = CurrentDb

fieldName = "TESTS" ' Update this value for the driving field

For Each tbl In db.TableDefs
    If tbl.Attributes = 0 Then   'This tells it to ignore hidden tables
        sqlString = "DELETE * FROM " & tbl.Name & " WHERE '" & fieldName & "' Is Null"
        DoCmd.RunSQL (sqlString)
    End If
Next

' Clean up the script
Set tbl = Nothing
Set db = Nothing

DoCmd.SetWarnings True ' Turn warnings back 'On'
End Sub