Table variable metadata can also be viewed in tempdb.sys.tables . This is easy to verify from the below.
declare @t2 as table ( [38F055D8-25D9-4AA6-9571-F436FE] int) SELECT t.name, t.object_id FROM tempdb.sys.tables t JOIN tempdb.sys.columns c ON t.object_id = c.object_id WHERE c.name = '38F055D8-25D9-4AA6-9571-F436FE'
Results Examples
name object_id ------------------------------ -----------
But you will notice that the name of the object is autogenerated and has nothing to do with the name of the variable.
If you do not have a unique, unique column name that you can use to filter as described above, and the table variable has at least one row in it, you can (starting with SQL Server 2008) use %%physloc%% and DBCC PAGE to determine this information. An example is below.
DECLARE @t2 AS TABLE ( a INT) INSERT INTO @t2 VALUES (1) DECLARE @DynSQL NVARCHAR(100) SELECT TOP (1) @DynSQL = 'DBCC PAGE(2,' + CAST(file_id AS VARCHAR) + ',' + CAST( page_id AS VARCHAR) + ',1) WITH TABLERESULTS' FROM @t2 CROSS APPLY sys.fn_PhysLocCracker( %% physloc %% ) DECLARE @DBCCPage TABLE ( [ParentObject] [VARCHAR](100) NULL, [Object] [VARCHAR](100) NULL, [Field] [VARCHAR](100) NULL, [VALUE] [VARCHAR](100) NULL ) INSERT INTO @DBCCPage EXEC (@DynSQL) SELECT VALUE AS object_id, OBJECT_NAME(VALUE, 2) AS object_name FROM @DBCCPage WHERE Field = 'Metadata: ObjectId'
source share