The Spanish government announced today that it will hold a referendum March 12 on its policy of keeping Spain within the Atlantic Alliance.

With polls showing deeply divided opinions among Spaniards over NATO, the announcement set the stage for the most heated political debate here since democracy was restored a decade ago following the death of Generalissimo Francisco Franco.

Deputy Prime Minister Alfonso Guerra said the question to be put to the voters in the referendum would be: "Do you consider it advisable for Spain to remain within the Atlantic Alliance under the terms agreed by the government."

Guerra said the government would regard the vote as "politically and morally binding."

This is the first time that any member of either the eastern or western blocs has put continued membership of a military alliance to a vote. The decision to do so in Spain is in accordance with a campaign pledge made by the Socialist prime minister, Felipe Gonzalez, during an election six months after Spain became a NATO member under a previous center-right administration in May 1982.

The all-important wording of the question and a preamble that will be printed on the ballot slips and that sets out the government's conditions for continued alliance membership are clearly intended to split the anti-NATO lobby.

The decision to hold the referendum on a weekday seeks to ensure a high turnout and block calls by the conservative opposition to abstain.

The wording was an explicit rejection of demands by the anti-NATO lobby, which is spearheaded by the Communist Party, that Spaniards be simply asked: "Do you want to remain in NATO or not?" Opinion polls have shown that a sizable majority of Spain's 26 million voters would vote for withdrawal from the alliance.

Guerra said the government's "terms" for continued membership are: Spain's nonmembership in NATO's integrated command structure, a ban on the entry and stockpiling of nuclear weapons in Spain and the "progressive reduction of the U.S. military presence in Spain."

These provisos seek to swing lukewarm Spanish opinion behind the Socialist government's about-face on the NATO issue. Gonzalez was vigorously opposed to Spain's entry into NATO four years ago and changed his mind about the alliance after national elections swept his Socialist Party to power in October 1982.

The nuclear ban was voted by the parliament at the time of Spain's entry into the alliance, and immediately after Gonzalez took power, the new government froze Spain's relations with NATO's military command structure.

The government's third condition, concerning U.S. troops in Spain, was the subject of a joint U.S.-Spanish statement in December that agreed to future talks over a "phased and negotiated" reduction.

Officials say they believe that the phrasing of the question and the preamble's conditions will ensure majority backing for the government's position. A December poll conducted by the government's polling agency showed that 31 percent of those polled backed Gonzalez's package for continued alliance membership while 26 percent said Spain should withdraw completely from NATO.

The conservative opposition, although staunchly behind NATO membership, opposes the holding of the referendum and is calling for a boycott. Manuel Fraga, the leader of Spain's center-right parties, called the referendum "unnecessary, inadequate, overly costly and a dangerous precedent."