gpt4 book ai didi

sql - 删除 SQL Server 数据库中的所有表,除了少数

转载 作者:太空狗 更新时间:2023-10-30 01:47:03 25 4
gpt4 key购买 nike

我的数据库中有大约 50 多个表,现在我想要删除数据库中的所有表,除了少数。

现在我所知道的是 sys.tables 是一个列出所有表的表,所以最初我运行了这样的查询

delete from sys.tables where name like '%DynamicSurgery' (or any other condition)

认为它可能会起作用。但正如我所料,它会抛出一个错误

Ad hoc updates to system catalogs are not allowed.

请问SQL Server有没有删除倍数的方法?

最佳答案

您可以使用动态查询来DROP所需的表:

DECLARE @ExecSQL AS NVARCHAR (MAX) = '';

SELECT @ExecSQL = @ExecSQL +
'DROP TABLE ' + QUOTENAME(S.name) + '.' + QUOTENAME(T.name) + '; '
FROM sys.tables T
JOIN sys.schemas S ON S.schema_id = T.schema_id
WHERE T.name LIKE '%DynamicSurgery'

--PRINT @ExecSQL
EXEC (@ExecSQL)

关于sql - 删除 SQL Server 数据库中的所有表,除了少数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38720170/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com