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

SET @adminUserName = N'ali'
SET @NewAdminUserName = 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 NOT 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 = @NewAdminUserName))
BEGIN
	SET @GroupMemberID = (SELECT     dbo.tblGroupMembers.GroupMemberID
							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 = 0) AND 
  									(dbo.tblUsers.IUserName = @NewAdminUserName))
	UPDATE [InstantMessagingDB].[dbo].[tblGroupMembers]  SET [IsThatMemberAdmin] = 1  WHERE GroupMemberID = @GroupMemberID
end  		