DECLARE @adminUserName NVARCHAR(50)
DECLARE @DeleteUserName NVARCHAR(50)
DECLARE @GroupID DECIMAL(18,0)

SET @adminUserName = N'ali'
SET @DeleteUserName = N'sonam'
SET @GroupID = 1


IF EXISTS(SELECT     dbo.tblGroupMembers.GroupMemberID, dbo.tblGroupMembers.GroupID, dbo.tblGroups.GroupName, dbo.tblGroupMembers.UserID, dbo.tblUsers.IUserName, 
                      dbo.tblGroupMembers.IsThatMemberAdmin
			FROM         dbo.tblGroupMembers INNER JOIN
								  dbo.tblUsers ON dbo.tblGroupMembers.UserID = dbo.tblUsers.IUserID INNER JOIN
								  dbo.tblGroups ON dbo.tblGroupMembers.GroupID = dbo.tblGroups.GroupID
			WHERE     (dbo.tblGroupMembers.GroupID = @GroupID) AND (dbo.tblGroupMembers.IsThatMemberAdmin = 1) AND 
  		(dbo.tblUsers.IUserName = @adminUserName))
AND EXISTS  (SELECT     dbo.tblGroupMembers.GroupMemberID, dbo.tblGroupMembers.GroupID, dbo.tblGroups.GroupName, dbo.tblGroupMembers.UserID, dbo.tblUsers.IUserName, 
                      dbo.tblGroupMembers.IsThatMemberAdmin
			FROM         dbo.tblGroupMembers INNER JOIN
								  dbo.tblUsers ON dbo.tblGroupMembers.UserID = dbo.tblUsers.IUserID INNER JOIN
								  dbo.tblGroups ON dbo.tblGroupMembers.GroupID = dbo.tblGroups.GroupID
			WHERE     (dbo.tblGroupMembers.GroupID = @GroupID) AND 	(dbo.tblUsers.IUserName = @DeleteUserName))		
 BEGIN
	DELETE FROM [InstantMessagingDB].[dbo].[tblGroupMembers]   WHERE  GroupID = @GroupID and UserID = (SELECT     IUserID  FROM         dbo.tblUsers  WHERE     (IUserName = @DeleteUserName))
 END
  		