Office中国论坛/Access中国论坛

标题: 如何用SQL写删除整个表的记录(保留原表的机构)? [打印本页]

作者: wangxy689    时间: 2011-3-17 07:48
标题: 如何用SQL写删除整个表的记录(保留原表的机构)?
本帖最后由 wangxy689 于 2011-3-17 07:49 编辑

我这样写为什么不对? DoCmd.RunSQL “delete * from 源数据表”  ,我发现能把新增的记录删除,以前的记录却删除不了(也就是不能删除真个表的记录),这是为什么?应该怎么写才对?
作者: roych    时间: 2011-3-17 09:14
不可能啊。这里又没有其它条件。。。传上附件看看吧。
作者: ttyu    时间: 2011-3-17 15:21
应该不会的
作者: ttyu    时间: 2011-3-17 15:21
数据源打开了吗
作者: wangxy689    时间: 2011-3-21 20:45
打开了。就是新增加的数据能够删除,以前的数据不能删除。
作者: wangxy689    时间: 2011-3-21 20:46
这是不是需要和FOXPRO 中的险select all 后再用删除?
作者: liaohk    时间: 2011-3-21 21:08
Deleting all data from an Access database

Sometimes it may be necessary to delete all the data in a database
while retaining the table structure. If done manually, this job can
quickly become tedious. If your database has many tables, the
following code will clear all the data in a hurry.

Dim ctr As Container, doc As Document, db As Database
Set db = CurrentDB()
Set ctr = db.Containers!Tables
For Each doc in ctr.Documents
   If Left$(doc.Name, 4) <> "MSys" Then 注释:Table is not a system table
        db.Execute "Delete [" & doc.Name & "].*" & _
           "From [" & doc.Name & "];"
   End If
Next doc

You might not want to delete data from linked tables. This can be easily
accommodated by checking the Connect property for each TableDef document.
The modified code reads:

Dim ctr As Container, doc As Document, db As Database
Set db = CurrentDb()
Set ctr = db.Containers!tables
For Each doc In ctr.Documents
   If Left$(doc.Name, 4) <> "MSys" And _
     db.TableDefs(doc.Name).Connect = "" Then
   注释:Table is not a system table or a linked table
     db.Execute "Delete [" & doc.Name & "].*" & _
       "From [" & doc.Name & "];"
   End If
Next doc

You must also have cascading updates/deletes enabled for this procedure
to clear the data from all tables. As an alternative, run the code
multiple times. On the first pass through the database, the tables on
one side of the relationship are cleared, allowing the remaining tables
to be cleared on the next pass.

This tip was contributed by Dr. Michael S. Stoner, Henri Kover, and Stephen Bond







欢迎光临 Office中国论坛/Access中国论坛 (http://www.office-cn.net/) Powered by Discuz! X3.3