In our database, there is a table with a column of XML datatype. The column was mandatory (not null), I needed to make it non-mandatory (null). I wrote a script similar to:
alter table [SomeSchema].[SomeTable] alter column [SomeColumn] XML null
I needed to write/test a rollback script, so I wrote one similar to:
alter table [SomeSchema].[SomeTable] alter column [SomeColumn] XML not null
The initial statement executed correctly. When I attempted to test/execute the rollback, I got the following error:
Msg 511, Level 16, State 1, Line 1
Cannot create a row of size 8082
which is greater than the allowable maximum row size of 8060. The
statement has been terminated.
The issue seems to relate to row overflow, which supposedly shouldn’t apply to XML types (but seems to in this circumstance). This issue only occurs when certain records are in the table (i.e. it’s affected by data), but it doesn’t seem to directly correlate to the size of the data. For example, one record had an XML length of 10,473 characters and triggered this issue; another record had an XML length of 159,072 characters and didn’t trigger the issue.
Setup:
- Windows XP SP3
- SQL Server 2008 R2
Our resident DB guru managed to find some code to fix the issue (see below), but still can’t explain why this is occurring/necessary. Our suspicion is that XML smaller than 8060 bytes will be stored in-row, while larger XML will be stored in LOB storage, and that somehow applying the [non-]mandatory constraint is causing the row-size to be increased beyond this limit, but SQL Server isn’t dealing with this gracefully. We’re still at a loss as to:
Any explanations welcome!
exec sp_tableoption '[SomeSchema].[SomeTable]', 'large value types out of row', 1; go alter table [SomeSchema].[SomeTable] drop constraint [ThePKConstraint] go alter table [SomeSchema].[SomeTable] add constraint [ThePKConstraint] primary key clustered ( [SomeIDColumn] asc, [SomeOtherIDColumn] asc ) go