MSPs have voted for full parliamentary scrutiny of plans to allow more than 100 public bodies to access personal data through an individual's NHS number.
The proposals would see organisations such as HMRC being able to see certain data on the NHS Central Register (NHSCR).
Opponents said the move amounted to identity cards "by the back door"
The Scottish government insisted privacy would be protected.
MSPs voted 65 to 60 in favour of a motion by Scottish Liberal Democrat leader Willie Rennie, as amended by the government.
It was one of the closest votes at Holryood for some time.
Everyone born in Scotland or registered with a GP north of the border has a Unique Citizen Reference Number (UCRN) held in the NHSCR.
The Scottish government said opening up access to NHSCR would have a number of advantages, such as helping to trace children missing from education, identifying foreign patients accessing the NHS, allowing people to access public services securely, and helping HMRC to complete the tax register.
It added that "only a limited amount of data would be shared" and medical records would not be part of the register.
However, opponents have warned there could be a risk of a massive data breach.
Critics have also questioned why the data should be shared with more than 100 public bodies, including Scottish Canals and Quality Meat Scotland.
Scottish Liberal Democrat leader Willie Rennie told MSPs full parliamentary scrutiny was needed of what he called a "super ID database".
He told MSPs: "The proposal has the potential to cover 120 organisations across the public sector.
"This matters because the current diffuse storage of information has an inbuilt protection from crime and misuse that would be lost with one super database shared across the public sector.
"We know the problem with putting all your eggs in one basket or putting all your savings in the one bank or business. We should be cautious when the government asks us to do the same now."
Mr Rennie also took issue with the UCRN.
He said: "To allow all organisations to share that number means we move from having a series of numbers to one, single, universal number.
"It leaves open the possibility that information can be searched, profiled and mined."
'Secure and accurate'
Mr Swinney said the government will consider all the submissions to its recent consultation on its proposals very carefully, and no decision has yet been taken.
He said privacy impact assessments would be a "necessary prerequisite" of any proposals put forward.
He added: "Decisions will also only be taken after there has been full parliamentary scrutiny of any proposals that we advance."
Mr Swinney said the "strictly controlled use" of the NHSCR would be the "most secure, accurate, privacy and user-friendly way" to ensure correct identification of Scottish taxpayers.
He said: "It is vital that we get implementation of the new income tax powers right.
"Following the transition period, for every 1% error - for every 1% of the Scottish taxpayer base that we cannot identify - that could cost this parliament's budget potentially £50m or more."
Scottish Conservative MSP Liz Smith said there were concerns about individual consent.
"There's no doubt the public will see this as a step too far and one which is really a move to introduce an identity card by the back door.
"To have powerful opposition from the British Medical Association, the Information Commissioner and the Scottish Council for Voluntary Organisations makes very clear the unease among experts in the field and this must surely make the Scottish government think again."
She added: "The NHS database has existed for 60 years and is currently accessible only by the NHS and local authorities.
"But the new proposals would mean that more than 100 public bodies would be able to access that data."
Labour MSP Dr Richard Simpson said he was concerned by a lack of patient consent and said the current NHS system was "not fit for purpose".
He cited 794 breaches in NHS Lothian over a two-year period relating to inappropriate access of data.
He said: "We have a situation whereby using your unique identifier, people can maliciously get access to NHS data.
"Linking one identifier with another is extremely dangerous and needs a full debate."