I think that you're probably asking if -(으)러 and -(으)려고 have the same meaning. Yes, they do, but they have different roles. They both mean with the intent/in order to. -(으)러 can only be used with the verbs 오다, 가다, and 다니다(come/go/frequent or attend) as -(으)러 오다, -(으)러 가다, and -(으)러 다니다 in only statement, question, suggestive, and command sentences. -(으)려고 can be used with all other verbs except in sentences that suggest doing an activity or give a command. For example; 머리가 아파서 약을 사러 약국에 가요. = My head hurts so I go/I'm going to the drugstore (with the intent/in order) to buy medicine. This is correct.
머리가 아파서 약국에 가려고 약을 사요. = My head hurts so (with the intent/in order) to go to the drugstore, I buy/I'm buying medicine. This is incorrect order because being able to purchase medicine, shouldn't be the causal factor that leads to arrival at the drugstore. 머리가 아파서 약을 사려고 약국에 가요. = My head hurts so (with the intent/in order) to buy medicine I go/I'm going to the drugstore. This is also correct. -(으)려고 can also be used with the 하다 verb as -(으)려고 하다 which means to be about to do. For example; 지금 가려고 해요. = I'm going/leaving now. or I'm about to go/leave now. So, in conclusion: your first sentence is correct, but the second wouldn't be because of the incorrect order of verbs. It should be: 머리가 아파서 약을 사려고 약국에 가요.