How can I get the actual type of a column or variable in t-sql?
I know about SQL_VARIANT_PROPERTY
, but this fails miserably for NVARCHAR(MAX)
:
DECLARE @foo1 NVARCHAR(10) = N'a'
SELECT SQL_VARIANT_PROPERTY(@foo1, 'BaseType') --works fine
DECLARE @foo2 NVARCHAR(MAX) = N'a'
SELECT SQL_VARIANT_PROPERTY(@foo2, 'BaseType') --fails with an error:
--Operand type clash: nvarchar(max) is incompatible with sql_variant
Is there anything else that is able to tell me if a variable contains a value of the type NVARCHAR(MAX)
?
Some background:
I am working on a procedure that should reorder the columns of a table: Rename the old table, create a new one, copy the data and drop the old one. In order to do this, all indexes, views, constraints, etc. need to be recreated on the new table.
I want to make sure that nothing gets lost in this automatic process. For that, I would like to copy most values from the relevant system-tables to a generic temp-table and compare the values after the reordering. This works perfectly fine now, but it fails when trying to detect the type of nvarchar(max)-columns.