我有一个通过查询远程 API 进行大量搜索的应用程序。结果被拉入我的数据层(SQL Server),他们坐在那里查看用户是否愿意与他们进一步交互。一旦用户开始使用该项目,它必须留在系统中,但其余结果完全没有用,只是占用空间。
我正在考虑创建一个任务,该任务将运行一个存储过程来删除任何未被另一个表引用的行。有没有办法做到这一点?
另一种措辞是:有没有办法执行删除语句,该语句会跳过由于违反参照完整性而导致错误的行?
编辑:感谢@Kos的新信息...我正在考虑的替代路径是在表中添加一个位列,如果该行最终被使用并且只是让我的任务删除标记为假的行,则将其标记为真。
为清楚起见,这里是对情况的概述。这些语法可能并不完美,但希望你能明白:
-- 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]