It is your right, providing you adhere to any contractual obligations, to keep your Spanish mobile number regardless of which mobile network you move to within Spain . This is called porting your number and needs to be requested at the time of signing up with a new provider. When requesting portability with the new network the process is slightly different from buying a new sim with a new number, you will be given your new SIM straight away but it will not work at this time. Your current SIM with your old provider will continue to work normally, within a few days you will recieve a text to tell you the date and time the porting will take place, at the time stated your old SIM will stop working and you need to swap out your old sim with your new SIM, service with the new network will now start with your original number. If for any reason your new provider is not able to port in your number you will be notified and your old SIM with your original provider will continue to operate normally. Porting is normally only refused if there is a mistake in the identification of the number owner or there is a financial or contractual issue with the original provider that needs to be addressed first. Once porting is complete your old SIM is useless and can be binned.
Be aware that once started, the porting process cannot be stopped, if you change your mind , you will have to wait for the number to port to the new provider then port it back to your original provider with a new SIM.