Try to stop your dog doing it as soon as he does. Search the cause and treat it. If it's an infection, it's easy. If it's psychological, you will need to find and treat the reason with a specialist.