For the USA:
In the Armed Forces EXCEPT the National Guard (Army or Air)
I, (
NAME), do solemnly swear (or affirm) that I will support and defend the Constitution of the United States against all enemies, foreign and domestic; that I will bear true faith and allegiance to the same; and that I will obey the orders of the President of the United States and the orders of the officers appointed over me, according to regulations and the Uniform Code of Military Justice. So help me God.
In the National Guard (Army or Air)
I, (
NAME), do solemnly swear (or affirm) that I will support and defend the Constitution of the United States and the State of (
STATE NAME) against all enemies, foreign and domestic; that I will bear true faith and allegiance to the same; and that I will obey the orders of the President of the United States and the Governor of (
STATE NAME) and the orders of the officers appointed over me, according to law and regulations. So help me God.
Note the little "
according to regulations and the Uniform Code of Military Justice" clause... orders, from whatever source (even the President), which violate regulations, etc, are exempted from obedience.
The UCMJ makes this clear in article 92:
892. ART. 92. FAILURE TO OBEY ORDER OR REGULATION
Any person subject to this chapter who--
(1) violates or fails to obey any lawful general order or regulation;
(2) having knowledge of any other lawful order issued by any member of the armed forces, which it is his duty to obey, fails to obey the order; or
(3) is derelict in the performance of his duties;
shall be punished as a court-martial may direct.
Uniform Code of Military Justice - UCMJ