{"id":151,"date":"2012-04-16T13:42:12","date_gmt":"2012-04-16T13:42:12","guid":{"rendered":"https:\/\/sqlserverlearner.azurewebsites.net\/2012\/04\/16\/what-are-the-maximum-number-of-columns-in-a-table-in-sql-server\/"},"modified":"2012-04-16T13:42:12","modified_gmt":"2012-04-16T13:42:12","slug":"what-are-the-maximum-number-of-columns-in-a-table-in-sql-server","status":"publish","type":"post","link":"https:\/\/sqlserverlearner.com\/2012\/04\/16\/what-are-the-maximum-number-of-columns-in-a-table-in-sql-server\/","title":{"rendered":"What are the maximum number of columns in a table in SQL Server"},"content":{"rendered":"

table.hovertable {
\nfont-family: verdana,arial,sans-serif;
\nfont-size:11px;
\ncolor:#333333;
\nborder-width: 1px;
\nborder-color: #999999;
\nborder-collapse: collapse;
\n}
\ntable.hovertable th {
\nbackground-color:#c3dde0;
\nborder-width: 1px;
\npadding: 8px;
\nborder-style: solid;
\nborder-color: #a9c6c9;
\n}
\ntable.hovertable tr {
\nbackground-color:#d4e3e5;
\n}
\ntable.hovertable td {
\nborder-width: 1px;
\npadding: 8px;
\nborder-style: solid;
\nborder-color: #a9c6c9;
\n}<\/p>\n

What are the maximum number of Columns in a table in SQL Server?<\/b>
\nSQL Server supports a maximum of 1,024 Columns in a nonwide table.
\nSQL Server supports a maximum of 30,000 Columns in a wide table.
\nSQL Server supports a maximum of 1,024 Columns in a base table.
\n<\/p>\n\n\n\n\n\n\n
Max Capacity For<\/th>\nSQL Server 2012<\/th>\nSQL Server 2008 R2<\/th>\nSQL Server 2008<\/th>\nSQL Server 2005<\/th>\n<\/tr>\n
\nmaximum number of Columns in a nonwide table in SQL Server\n<\/td>\n\n1,024 Columns\n<\/td>\n\n1,024 Columns\n<\/td>\n\n1,024 Columns\n<\/td>\n\n–\n<\/td>\n<\/tr>\n
\nmaximum number of Columns in a wide table in SQL Server\n<\/td>\n\n30,000 Columns\n<\/td>\n\n30,000 Columns\n<\/td>\n\n30,000 Columns\n<\/td>\n\n–\n<\/td>\n<\/tr>\n
\nmaximum number of Columns in a base table in SQL Server\n<\/td>\n\n–\n<\/td>\n\n–\n<\/td>\n\n–\n<\/td>\n\n1,024\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\nNow if you want to test the maximum number of columns limitation in a table you can use the below test script.<\/p>\n

Test Script:<\/strong>
\n[sql]
\n–DROP TABLE TestCreateMaximumColumnsTable
\nDECLARE @i INT = 2
\nDECLARE @SQLTABLE VARCHAR(MAX) <\/p>\n

SET @SQLTABLE = ‘CREATE TABLE TestCreateMaximumColumnsTable(COLUMN1 BIT)’<\/p>\n

EXEC(@SQLTABLE)
\nWHILE @i <= 1025
\nBEGIN
\nSELECT @SQLTABLE = ‘ALTER TABLE TestCreateMaximumColumnsTable
\n ADD COLUMN’+CAST(@i AS VARCHAR(5))+’ BIT’
\nEXEC(@SQLTABLE)
\nSET @i = @i + 1
\nEND
\n[\/sql]<\/p>\n

When the script tries to create 1025th column you get the below error:
\n
\nMsg 1702, Level 16, State 1, Line 1
\nCREATE TABLE failed because column ‘COLUMN1025’ in table ‘TestCreateMaximumColumnsTable’ exceeds the maximum of 1024 columns.
\n<\/font><\/p>\n

\nhttp:\/\/msdn.microsoft.com\/en-us\/library\/ms143432.aspx<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"

table.hovertable { font-family: verdana,arial,sans-serif; font-size:11px; color:#333333; border-width: 1px; border-color: #999999; border-collapse: collapse; } table.hovertable th { background-color:#c3dde0; border-width: 1px; padding: 8px; border-style: solid; border-color: #a9c6c9; } table.hovertable tr { background-color:#d4e3e5; } table.hovertable td { border-width: 1px; padding: 8px; border-style: solid; border-color: #a9c6c9; } What are the maximum number of Columns in a table in SQL…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[41,1500],"tags":[1501,1502,1503,1504,1505,1506,1507,1508,1509,1510,1511,1512,1513,1514,1515,1516,1517,1518,1519,1520,1521,1522,1523,1524,1525,1526,1527,1528,1529,1530,1531,1532,1533,1534,1535,1536,1537,1538,1539,1540,1541,1542,1543,1544,1545,1546,1547,1548,1549,1550],"_links":{"self":[{"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/posts\/151"}],"collection":[{"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/comments?post=151"}],"version-history":[{"count":0,"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/posts\/151\/revisions"}],"wp:attachment":[{"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/media?parent=151"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/categories?post=151"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/tags?post=151"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}