The General Assebly of the Church of Scotland has voted to allow the ordination of openly gay ministers but only after an emotional debate and pleas by traditionalists from all over the country to reject the move.

The General Assembly's final say in the historic change to Church law brought a muted response from the floor after more than two hours of debate.

The assembly saw 488 vote, and the church voted in favour of openly gay ministers 309-182.

It means allowing actively gay ministers in congregations who support the move in a "mixed economy" policy.

The Kirk said 21 out of 806 ministers have left the Kirk over the possibility of the Church agreeing to allow individual congregations to choose a gay minister.

The ordaining of ministers in same-sex relationships has divided the Church since traditionalist members attempted to block the appointment of Rev Scott Rennie, who is in a civil partnership, in Aberdeen in 2009.

This issue will be followed up on Thursday should the move be voted through, with the assembly being asked to include ministers in same-sex marriages in Church law.

Moderator Angus Morrison called for "moments of reflection".

During the annual gathering of the Church of Scotland, which has met almost every year since its inception in 1560, 730 commissioners drawn from congregations across Scotland and beyond will make historic decisions that will affect the direction of the national church.

The assembly began with the installation of the Moderator Rev Dr Morrison, who was forced to withdraw last year after being diagnosed with prostate cancer, but has now fully recovered.

In a speech later today, the outgoing Moderator Very Rev John Chalmers is expected to say: "We cannot go on suffering the pain of internal attacks which are designed to undermine the work or the place of others. It's time to play for the team.

"And let me be very clear here - I am not speaking to one side or another of the theological spectrum. I am speaking to both ends and middle.

"It is time to stop calling each other names, time to shun the idea that we should define ourselves by our differences and instead define ourselves by what we hold in common - our baptism into Christ, our dependence on God's grace, our will to serve the poor and so on."

Co-ordinator of the Principal Clerk's office, Very Rev David Arnott, said: "The General Assembly of the Church of Scotland decided today to allow individual Kirk Sessions the possibility of allowing a nominating Committee to consider an application from a minister living in a civil partnership.

"During a vacancy a Kirk Session may, but only if it so wishes, and after due deliberation, agree to a Nominating Committee accepting an application from such a minister. No Kirk Session may be coerced into doing so against its own wishes. This decision was in line with a majority of presbyteries who voted in favour of such a move."

Because the debate predates the legalisation of gay marriage the proposed change mentions only civil partnerships, not same-sex marriages.

The Assembly will be asked on Thursday to consider amending today's new Church law to include ministers in same-sex marriages.

Nicola Sturgeon MSP attended the opening of the General Assembly for the first time as First Minister.

She witnessed the installation of Rev Dr Angus Morrison as Moderator of the General Assembly, who had to withdraw from the role last year to undergo treatment for cancer.

She said: "(It was) a pleasure to attend the opening ceremony of the General Assembly and see Rt Rev Angus Morrison installed as Moderator."

This year's Lord High Commissioner - the Queen's representative at the Assembly - is Lord Hope of Craighead, a retired Scottish judge.

He read out a letter from the Queen to the Assembly which praised Scotland's national church for the role it played during last year's independence referendum, formation of the new Churches' Mutual Credit Union and the new "decade for ministry" strategy to recruit new ministers.