I am aware of no law that would prohibit a hospital from providing a social security number to a collection agency when it is incident to trying to collect an unpaid debt.
If your SSN was part of the billing information that the hospital had then this information would be transmitted to the collection agency. The hospital did not “sell” your SSN, it is part of the file that is transmitted to the debt collector.
DISCLAIMER: David J. McCormick is licensed to practice law in the State of Wisconsin and this answer is being provided for informational purposes only because the laws of your jurisdiction may differ. This answer based on general legal principles and is not intended for the purpose of providing specific legal advice or opinions. Under no circumstances does this answer constitute the establishment of an attorney-client relationship.