开发者

How can I check to see if row exists in two databases?

开发者 https://www.devze.com 2023-03-21 04:14 出处:网络
I\'m not quite sure on the correct way to see if a row exists in both databases. Please help. @pStoreNum bigint,

I'm not quite sure on the correct way to see if a row exists in both databases. Please help.

@pStoreNum bigint,
@pGuid varchar(100)

IF EXISTS (SELECT Guid FROM dbo.StoreSales WHERE Guid = @pGuid 
AND SELECT Guid F开发者_JAVA技巧ROM dbo.StoreReporting  WHERE Guid = @pGuid)
BEGIN
UPDATE
    dbo.StoreReporting 
SET 
    ValidCount = ValidCount + 1
WHERE
    StoreNum = @pStoreNum
END


You just need to specify EXISTS twice:

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


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

This will check that it's in both tables. The INNER JOIN will limit the result set to rows that have the same GUID in both tables, so if it exists in that JOINed result set it exists both places.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号