Dr Cormac Mac Amhlaigh, a lecturer in public law at Edinburgh University and an expert in EU constitutional law, said:-
"It's a very tricky question. Formally speaking, it's the UK that enjoys EU membership and the EU would negotiate with the UK for withdrawal, both the terms and any subsequent relationship.
Scotland is not an independent state, so it does not formally have a direct relationship with the EU - that occurs through the UK.
So formally speaking, it seems Scotland would have to follow the procedures of becoming an independent state first, and then apply for accession to the EU, under the conditions that are stipulated in treaties at the moment.
Now that's the formal position, but if the EU was willing, it is likely they could suspend that or amend that, and start forming negotiations with Scotland if they so wished.
The rules surrounding this are often quite open and flexible, and I think there's nothing to stop the EU institutions negotiating with Scotland, making some sort of transitional arrangement and making some sort of arrangement so Scotland can inherit the UK's EU membership.
It would be complex, it would be tricky, it would be quite unorthodox, but I think it's certainly not impossible.
And really what we would be relying on here would be the political will - the political will of the remaining EU state governments, the EU institutions and of course the Scottish government and whether a remaining British government would be in agreement. So it is quite a complex question, but not impossible.
I think this could only work as part of a transition to an independent Scotland."
www.bbc.co.uk/news/uk-scotland-scotland-politics-36643385