SQL Server Capacity Constraints – Dynamic Retrieval Methods

information-schemasql server

I have some code which creates database tables dynamically, and I want to ensure that people don't try to do things that result in me trying to create a table with more columns than what SQL Server allows.

According to this article, the limit for the max number of columns per (nonwide) table is 1024.

However, this can obviously change in the future, so I don't want to hardcode it in my app.

Is there a way to get this value dynamically using a SQL query (e.g. by reading from some system table or proc)?

Best Answer

No there's nothing built in that is exposed to us.

Your app would need to maintain some sort of configuration table itself.

You can't even try and parse it out of sys.messages as it isn't hardcoded into the text for message_id=1702.

CREATE TABLE failed because column '%.*ls' in table '%.*ls' exceeds the maximum of %d columns.

Your app could of course try and dynamically discover the limit by simply trying CREATE TABLE statements with ever greater amounts of columns but that is not a serious suggestion.