A sheriff who suggested endemic police corruption has sparked a rare row between Scotland's police and judiciary.

Sheriff Robert Dickson last year jailed a constable who tried to cover up the drink driving of a fellow officer.

But after doing so he hinted that the crime may have been part of a wider "perceived culture that officers are willing to prevent the arrest of a colleague".

Now it has emerged his words - made in court and protected by absolute privilege - provoked a highly unusual exchange of letters between Sir Stephen and Mr Dickson's superior, Sheriff Principal Brian Lockhart.

Copies of the correspondence uncovered by The Herald's sister paper, The National, show Sir Stephen challenge Mr Dickson's basis for the speculating on wider problems.

The chief constable said he believed the comments were "unsubstantiated". Mr Dickson responded that he believed Mr Dickson had "ample evidence" to make the inference.

The row - which ended in the two men essentially agreeing to disagree - was never intended for the public domain but was obtained by The National after a lengthy battle with the force under the Freedom of Information Act.

Mr Dickson had jailed former constable David Carmichael for seven months back December.

He did so because Mr Carmichael had failed to breathalyse fellow officer Daryl McKillion - who was suffering from depression and later killed himself - after reports he had been driving his car under the influence in Coatbridge in 2010.

Mr Carmichael told his partner, a junior officer, that it was wrong to grass a fellow cop. He later admitted this was wrong.

But what appears to have spurred Mr Dickson to believe there was a risk that Mr Carmichael's behaviour was widespread was that the whole incident stemmed from a 999 call from an off-duty officer who did not disclose she knew McKillion was in the police.

Sheriff Dickson, sentencing Mr Carmichael in December 2014, said:

"I hope that this is a one off instance of an officer concealing criminal activity.

"I am, however, seriously concerned that it is not.

"The off duty police officer who reported the apparent drunk driver, knew he was a serving police officer.

"She deliberately chose not to state his name or job when she phoned the police office to report what she had seen.

"She did so because she was concerned that if she revealed that the driver was a police officer that her report would not be dealt with appropriately."

However, Sheriff Principal Lockhart supported his subordinate.

In a letter to Sir Stephen, he said: "Sheriff Dickson was entitled to draw from the facts before him that this cover-up may not have been a one-off incident".

He added that Mr Dickson had every right to make the remarks, in order that "public confidence in the judicial system is not further damaged."

In a clear rebuke to Sir Stephen, he concluded: "To suggest otherwise fails to recognise the role of the judiciary".

A spokeswoman for Police Scotland said: "The correspondence clearly sets out the chief constable's rejection of the claim that such practice may have been widespread and his personal condemnation of any officer who wilfully neglects their duty and that all members of Police Scotland are expected to maintain the organisation's highest professional standards.

"It also clearly sets out the respect for the independent role of the judiciary but does express concern that the original comments by Sheriff Dickson could have been misconstrued."

Chief Superintendent Niven Rennie, president of the Association of Scottish Police Superintendents, said: "Sir Stephen was right to challenge the sheriff on making a sweeping generalisation from just one case. Of course, the police has rotten apples. But the insinuation was that there are a huge number of corrupt officers and you can't say that on the basis of one incident."

Calum Steele, general secretary of the Scottish Police Federation said: "The chief constable was rightly defending the reputation of the force and the sheriff principal was understandably defending the reputation of the judiciary."