我有一个存储过程,以我的while循环开头:
WHILE(SELECT COUNT(*)
FROM LinkDB
WHERE BaseValue = @Guid
AND 'PENDING WORKFLOW' = (SELECT TOP(1) Status
FROM BaseDB
WHERE UniqueIdentifier IN (SELECT LinkValue
FROM LinkDB
WHERE BaseValue = @Guid))) > 0
我有一个执行上述操作的C#程序,但是如果我查看数据库,它似乎有时会忽略while循环。可能是什么导致了这个?如果我单独运行select,它会返回一个大于0的值
答案 0 :(得分:1)
这是非确定性的 您无法保证在运行中获得相同的顶部(1)
SELECT TOP(1) Status
FROM BaseDB
WHERE UniqueIdentifier IN (SELECT LinkValue
FROM LinkDB
WHERE BaseValue = @Guid)
可能是
SELECT TOP(1) Status
FROM BaseDB
JOIN LinkDB
ON LinkDB.LinkValue = BaseDB.UniqueIdentifier
and LinkDB.BaseValue = @Guid
--where needed deterministic where
- 你确定整件事不是吗?
SELECT count(*)
FROM BaseDB
JOIN LinkDB
ON LinkDB.LinkValue = BaseDB.UniqueIdentifier
and LinkDB.BaseValue = @Guid
and BaseDB.Status = 'PENDING WORKFLOW'