12

我有以下我正在尝试运行的查询:

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 字符集不匹配错误。

这里有什么问题?

4

3 回答 3

13

正如你已经确认有些东西是 NVARchar'd ..cast the nvarchar to char 例如

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
于 2012-11-14T01:30:51.223 回答
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 隐式转换的字符数据类型,因此您只需要自己显式地转换它,但是要小心避免由于转换导致的任何字符丢失。

于 2012-11-13T05:40:22.730 回答
0

如果你犯了这个错误,你应该看看两个条件。

  1. 所有表的所有列名或别名必须相同
  2. 所有列的类型必须相同 TableA(col NVARCHAR2), TableB(col NVARCHAR2)
于 2016-03-26T11:58:24.160 回答