2013-04-29 202 views
2

我有以下表和變量轉換失敗

DECLARE @FunctionValue nvarchar(255) 
DECLARE @TSV_WithTarget TABLE 
     (
      Transition_Set_Variable_ID INT, 
      [Value] nvarchar(255), 
      TargetTable nvarchar(255), 
      TargetColumn nvarchar(255), 
      FunctionValue nvarchar(255) 
     ) 

表包含以下數據:

IF (SELECT FunctionValue 
    FROM @TSV_WithTarget 
    WHERE Transition_Set_Variable_ID = @TSV_Target_Counter 
    AND FunctionValue IS NOT NULL) IS NOT NULL 
BEGIN 
    SELECT FunctionValue 
    FROM @TSV_WithTarget 
END 

enter image description here

顯然,這個代碼就在這裏如果我取消註釋任何註釋掉的行,則會產生以下錯誤。

SET @FunctionValue = (SELECT CASE 
        --WHEN FunctionValue LIKE 'DATEADD%' THEN (SELECT FunctionValue 
        --           FROM @TSV_WithTarget 
        --           WHERE Transition_Set_Variable_ID = @TSV_Target_Counter) 
        WHEN FunctionValue LIKE 'GETDATE' THEN (SELECT GETDATE()) 
        --WHEN FunctionValue LIKE 'STORED PROCEDURE' THEN (SELECT Changed_In_SP 
        --             FROM BPE_T_VA_Transition_Set_Variable 
        --             WHERE Transition_Set_Variable_ID = @TSV_Target_Counter) 
        --WHEN FunctionValue LIKE 'SWITCH_USER' THEN (SELECT 'hersem') 
        WHEN FunctionValue LIKE 'VALUE' THEN (SELECT Name 
                  FROM BPE_T_VA_Value 
                  WHERE Value_ID = (SELECT Set_To_Value_ID 
                       FROM BPE_T_VA_Transition_Set_Variable 
                       WHERE Transition_Set_Variable_ID = @TSV_Target_Counter)) 
        WHEN FunctionValue LIKE 'VARIABLE' THEN (SELECT Value_ID 
                   FROM BPE_T_VA_Process_Instance_Value 
                   WHERE Variable_ID = (SELECT Set_To_Variable_ID 
                         FROM BPE_T_VA_Transition_Set_Variable 
                         WHERE Transition_Set_Variable_ID = @TSV_Target_Counter) 
                   AND Process_Instance_ID = @Process_Instance_ID) 
        --ELSE (SELECT FunctionValue) 
       END AS FV 
       FROM @TSV_WithTarget 
       WHERE Transition_Set_Variable_ID = @TSV_Target_Counter 
       AND FunctionValue IS NOT NULL) 

Msg 241, Level 16, State 1, Line 83 Conversion failed when converting datetime from character string.

我不知道這可能是什麼。

  • @FunctionValue的設置發生在WHILE循環中。

回答

1

CASE表達式的值必須是相同的類型。因此,有必要更換

SELECT GETDATE()CONVERT(nvarchar(10), GETDATE(), 101)Value_IDCAST(Value_ID AS nvarchar(255))

+0

'將nvarchar值'04/29/2013'轉換爲數據類型int.' – hermann 2013-04-29 11:05:53

+0

並替換CAST上的Value_ID(Value_ID AS nvarchar(255)) – 2013-04-29 11:08:22

+0

轉換失敗,請將其添加到您的原始答案。謝謝。 – hermann 2013-04-29 11:12:40

1

這裏的問題是,在結果表中的列FV可以得到不同的類型依賴於表中的行@TSV_WithTarget訂購。您應該將所有CASE結果轉換爲一種類型nvarchar

例如 - 第一個非NULL行是GETDATE因此SQL將FV定義爲datetime。等等。