gpt4 book ai didi

sql-server - 如何运行删除查询以跳过由另一个表的外键引用的记录?

转载 作者:行者123 更新时间:2023-12-05 00:00:00 24 4
gpt4 key购买 nike

我有一个通过查询远程 API 进行大量搜索的应用程序。结果被拉入我的数据层 (SQL Server),它们会在那里查看用户是否愿意与它们进一步交互。一旦用户开始使用该项目,它就必须留在系统中,但其余的结果完全没有用,只是占用空间。

我正在考虑制定一项任务,该任务将运行 sproc 以删除其他表未引用的任何行。有没有办法做到这一点?

另一种措辞方式是:有没有办法执行删除语句,该语句会跳过由于违反参照完整性而导致错误的行?

编辑:感谢来自@Kos 的新信息...
我正在考虑的替代路径是向表中添加一个位列,如果该行最终被使用并且只是让我的任务删除行标记为 false,则将其标记为 true。

为了清楚起见,这里是情况的概述。这些语法可能并不完美,但希望您能理解:

-- Where all the results get pulled down and held (The table I want to clean up every 2 hours or so)
CREATE TABLE [reservations].[DumpTable](
[utypeID] [bigint] IDENTITY(1,1) NOT NULL
-- Other columns
CONSTRAINT [PK_UnitTypesFound] PRIMARY KEY CLUSTERED
(
[utypeID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]


-- This is one of many other tables that might reference the dump table
CREATE TABLE [reservations].[OtherTables](
[memberID] INT NOT NULL,
[utypeID] BIGINT NOT NULL -- Need to Know if the dumptable is referenced here
CONSTRAINT [PK_MemberUnitTypes] PRIMARY KEY CLUSTERED
(
[memberID],
[utypeID]
) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

ALTER TABLE [reservations].[OtherTables] WITH CHECK ADD CONSTRAINT [FK_OtherTable_DumpTable] FOREIGN KEY([utypeID])
REFERENCES [reservations].[DumpTable] ([utypeID])

ALTER TABLE [reservations].[OtherTables] CHECK CONSTRAINT [FK_OtherTable_DumpTable]

最佳答案

我今天遇到了同样的问题,最后到了这里。最终我想出了一种不同的方法,更接近于劳伦斯正在寻找的 IMO,但也很可能慢得多,这在我自己的情况下不是问题。

所以基本上我使用一个游标来逐行删除我的行,如果发生约束错误,我将删除语句放在 try/catch 块中以恢复循环:

DECLARE @idc as int
DECLARE Contact_Cursor CURSOR FOR
SELECT ID
FROM ContactInfo;
OPEN Contact_Cursor;
FETCH NEXT FROM Contact_Cursor INTO @idc
WHILE @@FETCH_STATUS = 0
BEGIN
begin try
delete from ContactInfo where id = @idc;
FETCH NEXT FROM Contact_Cursor INTO @idc
end try
begin catch
FETCH NEXT FROM Contact_Cursor INTO @idc
end catch
END;
CLOSE Contact_Cursor;
DEALLOCATE Contact_Cursor;
GO

关于sql-server - 如何运行删除查询以跳过由另一个表的外键引用的记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12701861/

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