这是我正在使用的查询:
DELETE TB1.*, TB2.*
FROM TB1
INNER JOIN TB2 ON TB1.PersonID = TB2.PersonID
WHERE (TB1.PersonID)='2'
它在 MS Access 中运行良好,但在 SQL Server 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 中出现错误(','.附近的语法不正确)。
如何解决?请帮忙。
您不能DELETE
从多个表中使用单个表达式SQL 2005
- 或任何其他标准 SQL。Access
是这里的例外。
获得这种效果的最佳方法是FOREIGN KEYS
在表之间指定一个ON
DELETE
trigger
.
为什么你不使用 a DELETE CASCADE FK
?
这不能在一个声明中完成。您将不得不使用 2 个语句
DELETE FROM TB1 WHERE PersonID = '2';
DELETE FROM TB2 WHERE PersonID = '2';
据我所知,你不能在一个句子中做到这一点。
但是您可以构建一个存储过程,在事务中的任何表中执行您想要的删除,几乎相同。
我认为您不能一次从多个表中删除(尽管我不确定)。
但是,在我看来,最好通过级联删除的关系来实现此效果。如果您这样做,您将能够从一个表中删除记录,而另一个表中的记录将被自动删除。
例如,假设这两个表代表一个客户,以及客户的订单。如果您将关系设置为级联删除,您只需删除客户表中的记录,订单将自动删除。
请参阅有关级联引用完整性约束的 MSDN 文档。
为引用master主键的明细表指定外键并设置Delete rule = Cascade。
现在,当您从主表中删除一条记录时,所有其他基于删除行主键值的详细表记录将被自动删除。
因此,在这种情况下,主表的单个删除查询可以删除主表数据以及子表数据。
在程序中使用它
declare cu cursor for SELECT [name] FROM sys.Tables where [name] like 'tbl_%'
declare @table varchar(100)
declare @sql nvarchar(1000)
OPEN cu
FETCH NEXT FROM cu INTO @table
WHILE @@FETCH_STATUS = 0
BEGIN
set @sql = N'delete from ' + @table
EXEC sp_executesql @sql
FETCH NEXT FROM cu INTO @table
END
CLOSE cu;
DEALLOCATE cu;
我用它来清理测试/开发数据库中的数据。您可以按表名和记录数进行过滤。
DECLARE @sqlCommand VARCHAR(3000);
DECLARE @tableList TABLE(Value NVARCHAR(128));
DECLARE @TableName VARCHAR(128);
DECLARE @RecordCount INT;
-- get a cursor with a list of table names and their record counts
DECLARE MyCursor CURSOR FAST_FORWARD
FOR SELECT t.name TableName,
i.rows Records
FROM sysobjects t,
sysindexes i
WHERE
t.xtype = 'U' -- only User tables
AND i.id = t.id
AND i.indid IN(0, 1) -- 0=Heap, 1=Clustered Index
AND i.rows < 10 -- Filter by number of records in the table
AND t.name LIKE 'Test_%'; -- Filter tables by name. You could also provide a list:
-- AND t.name IN ('MyTable1', 'MyTable2', 'MyTable3');
-- or a list of tables to exclude:
-- AND t.name NOT IN ('MySpecialTable', ... );
OPEN MyCursor;
FETCH NEXT FROM MyCursor INTO @TableName, @RecordCount;
-- for each table name in the cursor, delete all records from that table:
WHILE @@FETCH_STATUS = 0
BEGIN
SET @sqlCommand = 'DELETE FROM ' + @TableName;
EXEC (@sqlCommand);
FETCH NEXT FROM MyCursor INTO @TableName, @RecordCount;
END;
CLOSE MyCursor;
DEALLOCATE MyCursor;
参考资料:
您可以使用以下内容:
DECLARE db_cursor CURSOR FOR
SELECT name
FROM master.dbo.sysdatabases
WHERE name IN ("TB2","TB1") -- use these databases
OPEN db_cursor
FETCH NEXT FROM db_cursor INTO @name
WHILE @@FETCH_STATUS = 0
BEGIN
DELETE FROM @name WHERE PersonID ='2'
FETCH NEXT FROM db_cursor INTO @name
END
CREATE PROCEDURE sp_deleteUserDetails
@Email varchar(255)
AS
declare @tempRegId as int
Delete UserRegistration where Email=@Email
set @tempRegId = (select Id from UserRegistration where Email = @Email)
Delete UserProfile where RegID=@tempRegId
RETURN 0
$qry = "删除 lg. , l. FROM Lessons_game lg RIGHT JOIN 课程 l ON lg.lesson_id = l.id WHERE l.id = ?";
课程是主表,而课程游戏是子表,所以右加入
DELETE TB1, TB2
FROM customer_details
LEFT JOIN customer_booking on TB1.cust_id = TB2.fk_cust_id
WHERE TB1.cust_id = $id
Try this query
DELETE TB1, TB2 FROM TB1 INNER JOIN TB2
WHERE TB1.PersonID = TB2.PersonID and TB1.PersonID = '2'