2012-11-13 60 views
8

我已經把下面的查詢,我試圖運行:字符集不匹配錯誤在甲骨文

SELECT Script from (

SELECT 9 as ColOrder, ' INSERT INTO PROJ VALUES(' || ID || ',''' || Name || ''', ''' || Version || ''', ''ABCD'', sysdate , ''ABCD'', sysdate);' as Script FROM PROJ where Name like '%[Param.1]%' 

union 

SELECT 11 as ColOrder,' INSERT INTO PROJMOD VALUES(' || ID || ', ' || ProjID || ', ' || ModID || ', ' || ObjID || ', ''ABCD'', sysdate, ''ABCD'', sysdate);' as Script FROM PROJMOD where ProjID in (select ID from PROJ where Name like '%[Param.1]%') 

) x 

Order by ColOrder 

但它給了我ORA-12704: character set mismatch error.

當我單獨運行這兩個select語句,它給了我正確的輸出,但是當我正在做兩個select的聯合時,它會給出tme字符集不匹配錯誤。

這裏有什麼問題?

+0

作爲colorder不是可合併的嘗試工會而不是工會。是否有任何列有nvarchar或nchar數據類型? – DazzaL

+0

是的,'ABCD'列是NVARCHAR2。我嘗試使用UNION ALL,但它給了我同樣的錯誤。 –

+0

它不會是文字ABCD,但一列(id projid或名稱等)看到我的答案的細節。 – DazzaL

回答

1
SELECT Script from (

SELECT 9 as ColOrder, ' INSERT INTO PROJ VALUES(' || to_char(ID) || ',''' || to_char(Name) || ''', ''' || to_char(Version) || ''', ''ABCD'', sysdate , ''ABCD'', sysdate);' as Script FROM PROJ where Name like '%[Param.1]%' 

union 

SELECT 11 as ColOrder,' INSERT INTO PROJMOD VALUES(' || to_char(ID) || ', ' || to_char(ProjID) || ', ' || to_char(ModID) || ', ' || to_char(ObjID) || ', ''ABCD'', sysdate, ''ABCD'', sysdate);' as Script FROM PROJMOD where ProjID in (select ID from PROJ where Name like '%[Param.1]%') 

) x 

Order by ColOrder 

我剛添加了TO_CHAR函數,那就行了。顯然你的字段數據類型不能由Oracle隱式轉換,所以你只需要自己明確地轉換它,但要小心避免由於轉換造成的任何字符丟失。

+1

你不需要也不應該在這種情況下使用union。使用「union all」 – DazzaL

10

爲您確認一些事情是NVARchar'd ..cast將nvarchar的燒焦如

SQL> create table tab(a nvarchar2(2)); 

Table created. 

SQL> insert into tab values ('a'); 

1 row created. 

SQL> select 1, 'hi' from dual 
    2 union all 
    3 select 2, a from tab; 
select 1, 'hi' from dual 
      * 
ERROR at line 1: 
ORA-12704: character set mismatch 

未能按 「A」 是NVARCHAR。所以TO_CHAR它:

SQL> select 1, 'hi' from dual 
    2 union all 
    3 select 2, to_char(a) from tab; 

     1 'HI' 
---------- ---- 
     1 hi 
     2 a 

或鑄造字符串文字「HI」到nvarchar的

SQL> select 1, n'hi' from dual 
     2 union all 
     3 select 2, a from tab; 

      1 N' 
    ---------- -- 
      1 hi 
      2 a 
0

如果採取這種錯誤,你應該看看兩個條件。

  1. 所有列名或別名必須爲所有表
  2. 所有列必須是同一類型的表A(COL NVARCHAR2)是一樣的,表B(COL NVARCHAR2)