title: Use NOT EXISTS in SQL Query?
body: <p>I have problem  with my stored procedure. I am trying to see if the group does not EXISTS by seeing of by using the <code>userID &amp; GroupName</code> I want to be able could not have same group user and could have same group name for different user </p>
  
  <pre><code>ALTER PROCEDURE [dbo].[Insert_Group]
     @UserID uniqueidentifier 
     ,@GroupName varchar(100)
     ,@OutGroupID int out 
  AS
  BEGIN
      -- SET NOCOUNT ON added to prevent extra result sets from
      -- interfering with SELECT statements.
      SET NOCOUNT ON;
  
      if not EXISTS(select Groups.id from Groups where Groups.name = @GroupName and Groups.userID = userID)
      begin 
         -- Insert statements for procedure here
         INSERT INTO [ideaPark_DB].[dbo].[Groups]([userID], [name])
         VALUES (@UserID, @GroupName)
  
         SELECT @OutGroupID = SCOPE_IDENTITY();
      end 
   else 
      SELECT @OutGroupID = (select Groups.id 
                            from Groups 
                            where Groups.name = @GroupName and Groups.userID = userID)
  END
  </code></pre>