我尝试在循环中填充表TBL。并在子查询上获取ORA-12704: character set mismatch
。
这是我使用的查询:
BEGIN
FOR i IN (SELECT t.Stuff FROM STUFF_TABLE t ORDER BY t.Name ASC)
LOOP
INSERT INTO TBL(StuffId, StuffName)
VALUES(
i.Stuff,
(SELECT TempStuffName FROM
(SELECT COALESCE(st.StuffName, i.Stuff) as TempStuffName FROM STUFFDEFINITION st WHERE st.Stuff = i.Stuff ORDER BY st.Version DESC)
WHERE ROWNUM = 1)
);
END LOOP;
END;
列类型如下:
STUFF_TABLE.Stuff nvarchar2(30)
TBL.StuffId nvarchar2(30)
TBL.StuffName nvarchar2(50)
STUFFDEFINITION.Stuff varchar2(255)
STUFFDEFINITION.StuffName varchar2(255)
根据我的理解,问题在于类型转换,即从varchar2(255)到nvarchar2(50)。
我尝试使用CAST,Translate函数,但它没有帮助。 ORA-12704: character set mismatch
仍然存在。
有没有办法在我尝试的时候在循环中填充TBL?
以下是重现问题的测试数据:
CREATE TABLE STUFF_TABLE
(
Stuff nvarchar2(30),
Name nvarchar2(50)
);
CREATE TABLE TBL
(
StuffId nvarchar2(30),
StuffName nvarchar2(50)
);
CREATE TABLE STUFFDEFINITION
(
Stuff varchar2(255),
StuffName varchar2(255),
Version number(19)
)
INSERT INTO STUFF_TABLE(Stuff, Name) VALUES('First', 'Name1');
INSERT INTO STUFF_TABLE(Stuff, Name) VALUES('Second', 'Name2');
INSERT INTO STUFF_TABLE(Stuff, Name) VALUES('Third', 'Name3');
INSERT INTO STUFFDEFINITION(Stuff, StuffName, Version) VALUES('First', 'First Stuff', 1);
INSERT INTO STUFFDEFINITION(Stuff, StuffName, Version) VALUES('First', 'First Stuff', 2);
INSERT INTO STUFFDEFINITION(Stuff, StuffName, Version) VALUES('Second', 'Second Stuff', 1);
INSERT INTO STUFFDEFINITION(Stuff, StuffName, Version) VALUES('Third', 'Third Stuff', 1);
答案 0 :(得分:2)
从您的陈述中可以清楚地看到,您正在使用varchar2
加入nvarchar2
列,并在varchar2
列中插入nvarchar2
列。您需要先进行转换。试试这个:
BEGIN
FOR i IN (SELECT t.Stuff FROM STUFF_TABLE t ORDER BY t.Name ASC)
LOOP
INSERT INTO TBL(StuffId, StuffName)
VALUES(
i.Stuff,
(SELECT to_nchar(TempStuffName)
FROM
(SELECT COALESCE(to_nchar(st.StuffName), i.Stuff) as TempStuffName
FROM STUFFDEFINITION st
WHERE to_nchar(st.Stuff) = i.Stuff
ORDER BY st.Version DESC)
WHERE ROWNUM = 1)
);
END LOOP;
END;
答案 1 :(得分:1)
您不应该使用PL / SQL:
INSERT INTO TBL ( StuffId, StuffName )
SELECT TO_NCHAR( t.stuff ),
TO_NCHAR(
COALESCE(
MAX( d.StuffName ) KEEP ( DENSE_RANK LAST ORDER BY d.version ),
t.stuff
)
)
FROM StuffTable t
LEFT OUTER JOIN StuffDefinition d
ON ( t.stuff = TO_NCHAR( d.stuff ) )
GROUP BY t.stuff;
答案 2 :(得分:0)
Oracle对此错误说:' INSERT语句的VALUES子句中的字符串表达式与插入值的列的字符集不同。'
请从nls_database_parameters中选择并告诉我它们的值是什么: NLS_CHARACTERSET NLS_NCHAR_CHARACTERSET NLS_RDBMS_VERSION NLS_LENGTH_SEMANTICS