设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

返回列表 发新帖
查看: 2649|回复: 6
打印 上一主题 下一主题

[查询] 如何用SQL写删除整个表的记录(保留原表的机构)?

[复制链接]
跳转到指定楼层
1#
发表于 2011-3-17 07:48:16 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 wangxy689 于 2011-3-17 07:49 编辑

我这样写为什么不对? DoCmd.RunSQL “delete * from 源数据表”  ,我发现能把新增的记录删除,以前的记录却删除不了(也就是不能删除真个表的记录),这是为什么?应该怎么写才对?
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 分享淘帖 订阅订阅
2#
发表于 2011-3-17 09:14:12 | 只看该作者
不可能啊。这里又没有其它条件。。。传上附件看看吧。
3#
发表于 2011-3-17 15:21:34 | 只看该作者
应该不会的
4#
发表于 2011-3-17 15:21:51 | 只看该作者
数据源打开了吗
5#
 楼主| 发表于 2011-3-21 20:45:14 | 只看该作者
打开了。就是新增加的数据能够删除,以前的数据不能删除。
6#
 楼主| 发表于 2011-3-21 20:46:00 | 只看该作者
这是不是需要和FOXPRO 中的险select all 后再用删除?
7#
发表于 2011-3-21 21:08:24 | 只看该作者
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


您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-11-15 06:40 , Processed in 0.078356 second(s), 30 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表