如何检查临时表中的列是否为nvarchar(max),如果是,请将其转换为或将其转换为nvarchar(4000),以便将其存储在sql_variant中? 以下是代码的一部分,其中@audit_oldvalue是sql_variant,而来自#tempTrigT的@Item是nvarchar(max):
set @sql = 'select @audit_oldvalue=[' +@Item +'] from #tempTrigT';
EXEC SP_EXECUTESQL @sql,N'@audit_oldvalue sql_variant OUTPUT',@audit_oldvalue OUTPUT
#tempTrigT:
Select * into #tempTrigT from (select * from deleted where @Action in ( 'U','D')) A UNION (select * from inserted where @Action ='I')
答案 0 :(得分:2)
set @sql = 'set @audit_oldvalue=(select cast([' +@Item +'] as NVARCHAR(4000)) from #tempTrigT)';
EXEC SP_EXECUTESQL @sql,N'@audit_oldvalue sql_variant OUTPUT',@audit_oldvalue OUTPUT
答案 1 :(得分:0)
在转换为SQL_VARIANT之前,只需将NVARCHAR(MAX)转换为NVARCHAR(4000)。
DECLARE @String NVARCHAR(MAX) = N'test string';
SELECT CAST(@String AS SQL_VARIANT); --fail
SELECT CAST(CAST(@String AS NVARCHAR(4000)) AS SQL_VARIANT); -- success