使用带有 WHERE 条件的单个查询 (SQL Express 2005) 从多个表中删除行
这是我正在使用的查询:
DELETE TB1.*, TB2.*
FROM TB1
INNER JOIN TB2 ON TB1.PersonID = TB2.PersonID
WHERE (TB1.PersonID)='2'
它在 MS Access 中工作正常,但在 SQL Server Express 2005 中出现错误(“,”附近的语法不正确。)。
如何解决?请帮忙。
This is the query I'm using:
DELETE TB1.*, TB2.*
FROM TB1
INNER JOIN TB2 ON TB1.PersonID = TB2.PersonID
WHERE (TB1.PersonID)='2'
It's working fine in MS Access but getting error (Incorrect syntax near ','.) in SQL Server Express 2005.
How to solve it? Please help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
您无法在
SQL 2005
或任何其他标准 SQL 中使用单个表达式从多个表中进行DELETE
。Access
是这里的例外。获得此效果的最佳方法是使用
ON
DELETE
trigger
在表之间指定FOREIGN KEYS
。You cannot
DELETE
from multiple tables with a single expression inSQL 2005
- or any other standard SQL for that matter.Access
is the exception here.The best method to get this effect is to specify
FOREIGN KEYS
between the table with anON
DELETE
trigger
.为什么不使用
DELETE CASCADE FK
?Why you don't use a
DELETE CASCADE FK
?这不可能在一份声明中完成。您将必须使用 2 个语句
This cannot be done in one statement. You will have to use 2 statements
据我所知,你不能用一句话来做到这一点。
但是您可以构建一个存储过程,在事务中的任何表中执行所需的删除操作,这几乎是相同的。
As i know, you can't do it in a sentence.
But you can build an stored procedure that do the deletes you want in whatever table in a transaction, what is almost the same.
我认为您不能一次从多个表中删除(尽管我不确定)。
然而,在我看来,最好通过级联删除的关系来实现这种效果。如果您这样做,您将能够从一个表中删除记录,而另一个表中的记录将被自动删除。
举个例子,假设这两个表代表一个客户,以及该客户的订单。如果你设置了级联删除的关系,你只需删除客户表中的记录,订单就会自动删除。
请参阅有关级联引用完整性约束的 MSDN 文档。
I don't think you can delete from multiple tables at once (though I'm not certain).
It sounds to me, however, that you would be best to achieve this effect with a relationship that cascades deletes. If you did this you would be able to delete the record from one table and the records in the other would be automatically deleted.
As an example, say the two tables represent a customer, and the customer's orders. If you setup the relationship to cascade deletes, you could simply delete record in the customer table, and the orders would get deleted automatically.
See the MSDN doc on cascading referential integrity constraints.
为明细表指定外键,引用主表的主键,并设置删除规则 = Cascade 。
现在,当您从主表中删除一条记录时,基于删除行主键值的所有其他详细表记录将被自动删除。
因此,在这种情况下,主表的单个删除查询可以删除主表数据以及子表数据。
Specify foreign key for the details tables which references to the primary key of master and set Delete rule = Cascade .
Now when u delete a record from the master table all other details table record based on the deleting rows primary key value, will be deleted automatically.
So in that case a single delete query of master table can delete master tables data as well as child tables data.
在程序中使用它
Use this in procedure
我不知道为什么人们让这件事变得如此困难。可以从多个表中删除行。这是我有效的查询语句。
客户表有客户的姓名。 client_horses 是客户拥有多少匹马的多行,equine_notes 是我对特定马匹所做的注释。我可以使用 INNER JOIN 子句通过一个查询语句删除与该客户端相关的所有内容。
希望这有帮助
I'm not sure why folks make this so difficult. It is possible to DELETE rows from multiple tables. Here is my query statement that works.
The clients table has the client's name. The client_horses are multiple rows of how many horses the client owns and equine_notes are notes that I have made on specific horses. I can delete everything pertaining to this client with the one query statement using the INNER JOIN clause.
Hope this helps
我用它来清理测试/开发数据库中的数据。您可以按表名称和记录数进行过滤。
参考信息:
sysindexes
I use this for cleaning up data in test/development databases. You can filter by table name and record count.
Reference info:
sysobjects
sysindexes
通常,我使用一个查询从多个表中进行删除。
它适用于 PostgreSQL,但不适用于 MSSQL,这就是我来这里的原因。
使用Postgres(我不知道其他数据库),你可以这样做:
实际上,在我原来的查询中,我从另外两个表中删除了外键,但在这里我只是粘贴了一个示例。
这样我解决了“PR_TO_COMMIT”表中的外键问题。
Generally I do deletions from multiple tables with one query.
It works correct with PostgreSQL, but doesn't work for MSSQL, and that's why I got here.
With Postgres (I don't know about other DBs) you can do:
Actually In my original Query I delete foreign keys from 2 more tables, but here I just paste an example.
That way I solved problem with Foreign Keys in "PR_TO_COMMIT" table.
我使用的方式从 SQL Server 中的多个表中删除行?
最重要的是在表中创建外键时使用ondeletecascade
The way I am using to Delete rows from multiple tables in SQL Server?
The most important is to use on delete cascade when creating a foreign key in the table
您可以使用类似以下内容:
You can use something like the following:
尝试这个查询
Try this query
$qry = "从 Lesson_game 中删除 lg., l. lg 在 lg.lesson_id = l.id 上右连接课程 l,其中 l.id = ?";
课程是主表和
Lessons_game 是子表,因此右连接
$qry = "DELETE lg., l. FROM lessons_game lg RIGHT JOIN lessons l ON lg.lesson_id = l.id WHERE l.id = ?";
lessons is Main table and
lessons_game is subtable so Right Join