Why are DDL changes to a temporary table not allowed within a snapshot transaction, especially when the temporary table is created within the snapshot transaction?
I understand why DDL changes to a shared object (such a user table or a global temporary table) should be disallowed within a snapshot transaction.
But why are DDL changes to a temporary table not allowed within a snapshot transaction? Isn't it true that by definition a temporary table is visible only to the connection that created it, and thus a temporary table created within a transaction is visible
only to that transaction (until the transaction is completed)? Why then are writes to a temporary table versioned at all under snapshot isolation? I think that DDL changes should be allowed on a temporary table within a snapshot transaction.
For example, I don't understand why the following code should not be allowed:
SET TRANSACTION ISOLATION LEVEL SNAPSHOT
BEGIN TRAN
CREATE TABLE #DefValues (Placeholder bit)
DECLARE @SQL varchar(MAX)
SET @SQL =
'ALTER TABLE #DefValues ADD Test1 int' + CHAR(10) +
'ALTER TABLE #DefValues ADD Test2 varchar(20)'
EXEC (@SQL)
SELECT * FROM #DefValues
COMMIT TRAN
DROP TABLE #DefValues
SET TRANSACTION ISOLATION LEVEL READ COMMITTED
This fails with:
Msg 3964, Level 16, State 1, Line 1
Transaction failed because this DDL statement is not allowed inside a snapshot isolation transaction. Since metadata is not versioned, a metadata change can lead to inconsistency if mixed within snapshot isolation.
I understand the limitation, but not why it has to exist. Can anyone comment on why this limitation could not be eased?
(Comments pertain to SQL 2005 through SQL 2008R2)