home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.databases.ms-sqlserver      Notorious Rube Goldberg contraption      19,505 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 17,997 of 19,505   
   tshad to All   
   Select with nulls   
   12 Nov 10 21:13:49   
   
   From: tfs@dslextreme.com   
      
   I need to check to see if a record exists to determine whether to insert a   
   record or not.   
      
   If I have the following:   
      
     SELECT *   
     FROM Image   
     WHERE nUserID = @UserID AND   
       nEventID = @EventID AND   
       nDocumentID = @DocumentID   
      
   if @@ROWCOUNT = 0   
      INSERT ...   
      
   The problem is that one or 2 of the parameters can be null.  @UserID cannot   
   be null.   
      
   If @UserID = 1234, @EventID = 1111, @DocumentID = NULL   
      
   I want it to return the record where @UserID is 1234, @EventID is 1111 and   
   @DocumentID is NULL.   
      
   If @UserID = 1234, @EventID = 1111, @DocumentID = 2222   
      
   I want it to return the record where @UserID is 1234, @EventID is 1111 and   
   @DocumentID is 2222   
      
   I thought about changing it to:   
      
    SELECT *   
     FROM Image   
     WHERE nUserID = @UserID AND   
                   ((@EventID is NULL AND nEventID is null)  OR (@EventID IS   
   NOT NULL AND nEventID = @EventID)) AND   
                   ((@DocumentID IS NULL AND nDocumentID is null)  OR   
   (@DocumentID IS NOT NULL AND nDocumentID = @DocumentID))   
      
   if @@ROWCOUNT = 0   
      INSERT ...   
      
   Is this the best way to do this?   
      
   Thanks,   
      
   Tom   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca