如何查看两个数据库中是否存在行?

时间:2011-07-19 16:10:54

标签: sql sql-server

我不太确定正确的方法来查看两个数据库中是否存在行。请帮忙。

@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

2 个答案:

答案 0 :(得分:3)

您只需要指定两次EXISTS:

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

答案 1 :(得分: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的行,因此如果它存在于JOIN个结果集中,则它存在于两个位置。