2011-07-19 95 views
1

我不太確定正確的方法來查看兩行數據庫中是否存在行。請幫忙。如何檢查兩行數據庫中是否存在行?

@pStoreNum bigint, 
@pGuid varchar(100) 

IF EXISTS (SELECT Guid FROM dbo.StoreSales WHERE Guid = @pGuid 
AND SELECT Guid FROM dbo.StoreReporting WHERE Guid = @pGuid) 
BEGIN 
UPDATE 
    dbo.StoreReporting 
SET 
    ValidCount = ValidCount + 1 
WHERE 
    StoreNum = @pStoreNum 
END 

回答

3

你只需要指定EXISTS兩次:

IF EXISTS (first select) AND EXISTS (second select) 
BEGIN 
... 
END 
+1

+1 - 比'INNER JOIN' – JNK

+0

感謝亞倫更具擴展性!我也給你一個贊成JNK。 –

1
IF EXISTS (SELECT Guid FROM dbo.StoreSales ss 
      INNER JOIN dbo.StoreReporting sr 
       ON sr.Guid = ss.Guid 
      WHERE ss.Guid = @pGuid) 

這將檢查它的兩個表所示。 INNER JOIN將結果集限制爲兩個表中具有相同GUID的行,因此如果它存在於該結果集中,則它存在兩個位置。

相關問題