Insomnia may be treated by medications, either to address the insomnia or to address its cause. For example, if pain is causing sleeping problems, medications for relieving pain may be prescribed.
Lifestyle modification is always the first management for insomnial. If it doesn't work, you can consult your doctor for prescription of medications. There are side effects associated with taking these kinds of medications for long periods of time. In general, they should only be taken for 2 to 4 weeks. You need to avoid alcohol and some other medications while using them. Talk to your doctor if you have any queries about your medications.