0

我有幾列表nvarchar(max)列。我正在構建一個觸發器,它嘗試將單元格存儲在sql_Variant中。 Nvarchar(max)不能存儲在sql_variant中。我如何檢查它是否爲nvarchar(max),然後將其轉換爲nvarchar(4000),以便我可以避免這種情況?SQL觸發器:更改nvarchar大小

編輯:最有可能發生錯誤的位置:

set @sql = 'select @audit_oldvalue=[' [email protected] +'] from #tempTrigT'; 

因爲@audit_oldvalue是SQL_VARIANT但[ '+ @ +項目']從#tempTrigT可以爲nvarchar(最大)

如果我編輯輸入表的東西我得到這個錯誤:

operand type clash nvarchar(max) is incompatible with sql_variant

表結構:

輸入表

enter image description here

輸出表

enter image description here

代碼,我想轉換:

Select * into #tempTrigT from (select * from deleted where @Action in ('U','D')) A UNION (select * from inserted where @Action ='I') 

Select @sql = @sql + 'Case when IsNull(i.[' + Column_Name + 
'],0) = IsNull(d.[' + Column_name + '],0) then '''' 
else ' + quotename(Column_Name, char(39)) + ' + '',''' + ' end +' 
from information_schema.columns 
where table_name = 'Forms' and column_name <>'rowguid' and column_name <>'modifieddate' 
--Define output parameter 
set @ParmDefinition = '@OutString varchar(max) OUTPUT' 
--Format sql 
set @sql = 'Select @OutString = ' 
+ Substring(@sql,1 , len(@sql) -1) + 
' From dbo.Forms i ' --Will need to be updated for target schema 
+ ' inner join #tempTrigT d on 
i.id = d.id' --Will need to be updated for target schema 
--Execute sql and retrieve desired column list in output parameter 
exec sp_executesql @sql, @ParmDefinition, @OutString OUT 


DECLARE @Items VARCHAR(max) 

set @Items = @OutString; 

DECLARE @Item VARCHAR(50) 
DECLARE @Pos INT 
DECLARE @Loop BIT 
SELECT @Loop = CASE WHEN LEN(@OutString) > 0 THEN 1 ELSE 0 END 
WHILE (SELECT @Loop) = 1 
BEGIN 
SELECT @Pos = CHARINDEX(',', @OutString, 1) 
IF @Pos > 0 
BEGIN 
SELECT @Item = SUBSTRING(@OutString, 1, @Pos - 1) 
SELECT @OutString = SUBSTRING(@OutString, @Pos + 1, LEN(@OutString) - @Pos) 
---------------------------------- 

set @audit_field = @Item; 

set @sql = 'select @audit_oldvalue=[' [email protected] +'] from #tempTrigT'; --ERROR LINE 
EXEC SP_EXECUTESQL @sql,N'@audit_oldvalue sql_variant OUTPUT',@audit_oldvalue OUTPUT -- If inserted @audit_oldvalue gets the new value 

set @sql = 'select @audit_value=i.[' [email protected] +'] from dbo.Forms i inner join #tempTrigT d on i.id = d.id'; 
EXEC SP_EXECUTESQL @sql,N'@audit_value sql_variant OUTPUT',@audit_value OUTPUT 


if @Action = 'U' 
begin 
    insert into [dbo].[AuditTrailForms]([TSid],[TSField],[OldValue],[NewValue],[changedate],[Change_Action],[Change_user],[Columns_Updated]) 
    select id,@audit_field, @audit_oldvalue, @audit_value,getdate(),@Action, coalesce(ModifiedBy,suser_name()), @Items 
    from inserted 

end 

    END 
ELSE 
BEGIN 
SELECT @Item = @OutString 
SELECT @Loop = 0 
END 
END 
+0

您使用哪種dbms? – jarlh

+0

@jarih sql server 2012 – aggicd

+0

你嘗試過「CONVERT(nvarchar(4000),@ nvarcharmaxparam)」嗎? –

回答

1

你應該能夠只投它作爲VARCHAR( 4000)。

Select @sql = @sql + 'Case when IsNull(CAST(i.[' + Column_Name + 
'] AS VARCHAR(4000)),0) = IsNull(CAST(d.[' + Column_name + '] AS VARCHAR(4000)),0) then '''' 
else ' + quotename(Column_Name, char(39)) + ' + '',''' + ' end +' 
from information_schema.columns 
where table_name = 'Forms' and column_name <>'rowguid' and column_name <>'modifieddate' 

或者,如果你想只對那些列做到這一點(我不相信它會的問題,除非出於性能的考慮)。

Select @sql = @sql + 'Case when IsNull(' 
    + CASE WHEN CHARACTER_MAXIMUM_LENGTH = -1 THEN 'CAST(' ELSE '' END 
    + 'i.[' + Column_Name + ']' 
    + CASE WHEN CHARACTER_MAXIMUM_LENGTH = -1 THEN ' AS VARCHAR(4000))' ELSE '' END + 
    ',0) = IsNull(' 
    + CASE WHEN CHARACTER_MAXIMUM_LENGTH = -1 THEN 'CAST(' ELSE '' END 
    + 'd.[' + Column_name + ']' 
    + CASE WHEN CHARACTER_MAXIMUM_LENGTH = -1 THEN ' AS VARCHAR(4000))' ELSE '' END + 
    + ',0) then '''' else ' 
    + quotename(Column_Name, char(39)) + ' + '',''' + ' end +' 
from information_schema.columns 
where table_name = 'Forms' and column_name <>'rowguid' and column_name <>'modifieddate' 
+0

OP已發佈有關此代碼多次的問題,儘管存在不同的問題但未接受任何答案到目前爲止(我見過)。不確定OP是否清楚實際需要的內容。 (道歉,如果不是在正確的地方,沒有足夠的代表,所以只能評論我自己的帖子) – PreQL

+0

謝謝你的答案。我提出了不同的問題,因爲在同一段代碼中有不同的主題。當然,我已經接受了問題。 – aggicd

+0

我測試了第二個片段,但我又收到相同的錯誤。我不想在VARCHAR(4000)中轉換所有東西,這就是爲什麼我選擇了片段#2 – aggicd