In the course of a discussion about prayers said in error, the Gemara quotes Rav Nahman as relating:
When we were in the school of Rabba bar Avuha we raised a dilemma before him:
Those students who mistakenly recited a blessing from the weekday Amida on Shabbat, what is the ruling with regards to completing the weekday prayer? And Rabba bar Avuha said to us: The ruling is that one must complete that entire blessing.
The Gemara explains:
On Shabbat, the individual is obligated and should actually recite all eighteen blessings, and it is the Sages who did not impose upon him in deference to Shabbat and instituted an abridged formula.
According to this opinion, Shabbat prayer is an abbreviated version of the weekday prayer, and therefore one who recites the weekday Amida on Shabbat and includes mention of Shabbat in his prayer, has, for all intents and purposes, arrived at the essence of the prayer and has fulfilled his obligation.
Thus, one who was reciting the morning, afternoon, or evening prayer on Shabbat and mistakenly began to recite a blessing from the corresponding weekday Amida, completes the blessing that he began and then continues with the Shabbat prayer, in accordance with the opinion of Rabba bar Avuha.
There is one Shabbat prayer that may be different. If one began to recite a weekday blessing during the musaf prayer, which would not be recited on a typical weekday, some say that he does the same as he does in the rest of the Shabbat prayers. The Ra’avad, however, rules that he stops immediately and continues with the additional prayer, since there is no connection between weekday blessings and the musaf prayer.
The later poskim ruled in accordance with the latter opinion to avoid a possible blessing in vain (see Mishna Berurah; Rambam Sefer Ahava, Hilkhot Tefilla 10:7; Shulḥan Aruk, Oraḥ Ḥayyim 268:2).