【发布时间】:2020-12-28 02:26:52
【问题描述】:
我有一个更新 Star 类型的存储过程。数据库表starValList 有一个外键用于表galaxyValList。该键是galaxyID。
所以我需要创建一个新的 GalaxyID 值,如果它是 null 或空 GUID。
所以我试试这个:
IF(@galaxyID IS NULL OR @galaxyID = '00000000-0000-0000-0000-000000000000')
BEGIN
SELECT @galaxyID=NEWID()
END
UPDATE starValList SET
[starIRR]= @starIRR,
[starDesc] = @starDesc,
[starType] = @starType,
[galaxyID]=@galaxyID
WHERE [starID] = @starID;
它适用于 starValList 表!
但我认为它也因为这个错误而失败:
The UPDATE statement conflicted with the FOREIGN KEY constraint "FK_starValList_galaxyValList". The conflict occurred in database "Astro105", table "dbo.galaxyValList", column 'galaxyID'.
失败是因为在galaxyValList 表中可能还没有该特定星系的条目。
但我仍然需要galaxyValList中的行,因为它可以稍后使用。
如何修复我的存储过程,使其不会产生此错误?
谢谢!
【问题讨论】:
-
通过从您的表
dbo.galaxyValList中获取正确的值。将NEWID用于表starValList中galaxyID的值实际上有100% 的机会生成galaxyValList中不存在的值 -
旁注,一年多来,SQL Server 2008 完全不受支持,您应该尽快查看升级路径。
标签: sql-server tsql sql-server-2008