简体   繁体   English

使用CAST通过emb-sql更改rpgle上的文本字段CCSID

[英]Using CAST to change text field CCSID on rpgle with emb-sql

I have a file defined this way: 我有这样定义的文件:

A          R RCONVTXT                                            
A            TEXT         100A         COLHDG('Text')            
A            TEXT2B       100G         COLHDG('Text')            
A                                      CCSID(1200 *NOCONVERT)    

I'm working with characters in polish language stored in the "TEXT" field. 我正在使用“ TEXT”字段中存储的波兰语字符。

If I use this code in my rpgle program: 如果我在rpgle程序中使用以下代码:

exec sql                                           
   UPDATE CONVTXT set TEXT2B =                    
   CAST(CAST(TEXT as char(100) CCSID 65535)       
   AS CHAR(100) CCSID 870);                        

all the text in first field "TEXT" is converted perfectly and updated in the other field in unicode. 第一个字段“ TEXT”中的所有文本均已完美转换,并在unicode的另一个字段中进行了更新。

But if the text that I want to convert is in a text field (100c), how can I convert it using SQL? 但是,如果要转换的文本位于文本字段(100c)中,如何使用SQL进行转换?

RPG will automatically convert between CCSID's. RPG将自动在CCSID之间转换。 This is all it takes: 这就是所有步骤:

**free
dcl-s ucs2str     Ucs2(100) Inz('this is a test');
dcl-s charstr     Varchar(100) Inz('');

charstr = ucs2str;

Here is a memory dump of ucs2str : 这是ucs2str的内存转储: 在此处输入图片说明

Here is a memory dump of charstr after the assignment: 这是赋值后charstr的内存转储: 在此处输入图片说明


Here is a little more info on this topic. 这里是有关此主题的更多信息。 TL/DR The following code works, and just a side note, our system is set to CCSID 65535. That isn't necessarily a good choice, it is just the way things are. TL / DR以下代码可以正常工作,并且仅需注意一点,我们的系统设置为CCSID65535。这不一定是一个好选择,这只是事实。

exec sql
  create table jmmlib/mytable
    (charfld   Char(100) ccsid 37,
     ucs2fld   NChar(100));

exec sql
  insert into jmmlib/mytable
    values('Constant Test', 'Constant Test'),
          (:ucs2str, :ucs2str),
          (:charstr, :ucs2str);

exec sql
  declare c1 cursor for
    select cast(ucs2fld as char(100) ccsid 37), charfld from jmmlib/mytable;

exec sql
  open c1;

exec sql
  fetch c1 into :ucs2str, :charstr;    ((1))

exec sql
  fetch c1 into :charstr, :ucs2str;    ((2))

exec sql
  fetch c1 into :ucs2str, :charstr;    ((3))

exec sql
  close c1;

So here things are a bit jumbled to help keep things seperate. 因此,这里有些杂乱无章的东西有助于保持事物的独立性。 I wouldn't necessarily code it this way normally. 我不一定会以这种方式正常编码。 The table columns are in the following order (UCS2, CHAR). 表列按以下顺序(UCS2,CHAR)。 The fetch columns are in the following order (CHAR, UCS2). 提取列的顺序如下(CHAR,UCS2)。

First look at the insert. 首先看一下插入。 I can insert constants in each of the fields, and the character sets are converted properly. 我可以在每个字段中插入常量,并且字符集可以正确转换。 I can insert a UCS2 string into either the UCS2 field or the CHAR field. 我可以将UCS2字符串插入UCS2字段或CHAR字段。 But, I can only insert the CHAR field into the CHAR field. 但是,我只能将CHAR字段插入CHAR字段。 There appears to be some issue with converting between 65535 and UCS2. 在65535和UCS2之间进行转换似乎存在一些问题。 I believe that this is an issue for me because our box has the QCCSID system value set to 65535. This is true even though the default CCSID for our jobs is 37. I do not think this would be an issue if QCCSID was set some other way. 我认为这对我来说是个问题,因为我们的包装盒的QCCSID系统值设置为65535。即使我们作业的默认CCSID为37,也是如此。如果将QCCSID设置为其他值,我认为这不会成为问题。方式。

Next look at the declaration for cursor C1 . 接下来看一下游标C1的声明。 I have cast UCS2FLD to CCSID 37. This is the only way I could get FETCH ((2)) to work. 我已经将UCS2FLD转换为CCSID37。这是我可以使FETCH((2))工作的唯一方法。 This was that conversion issue again. 这又是那个转换问题。 CCSID 37 can likely be put into a 65535 field because it is an EBCDIC CCSID, so the field is still EBCDIC, even though no conversion happens, and RPG is ok with that (or SQL since it was an SQL error message). CCSID 37可以放入65535字段中,因为它是EBCDIC CCSID,所以即使没有发生任何转换,该字段仍然是EBCDIC,并且RPG可以接受(或SQL,因为它是SQL错误消息)。 But it can't put UCS2FLD into an EBCDIC field without converting it first, and it can't convert from UCS2 to CCSID 65535. Once again, I don't think this would be a problem if we weren't using CCSID 65535. 但是,如果不先将UCS2FLD转换,就不能将UCS2FLD放入EBCDIC字段,并且也不能从UCS2转换为CCSID65535。再次,如果我们不使用CCSID 65535,我认为这不会成为问题。

Are you asking how you use an RPG defined variable in embedded SQL? 您是否在问如何在嵌入式SQL中使用RPG定义的变量?

If so, the answer is fairly simple. 如果是这样,答案很简单。 You simply need to put a leading ':' in front of the RPG variable in the SQL statement. 您只需要在SQL语句的RPG变量前面加上一个前导“:”即可。

Dcl-S Text Char(100) Inz('blah');

Exec Sql                                           
   Update ConvTxt Set Text2B =                    
   Cast(Cast(:Text As Char(100) CCSID 65535)       
   As Char(100) CCSID 870); 

Also, you can set a character string to a particular CCSID using the following which may be an even better solution to your problem. 另外,您可以使用以下命令将字符串设置为特定的CCSID,这可能是解决问题的更好方法。

Dcl-S Text Char(100) CCSID(870) Inz('blah');

Exec Sql                                           
   Update ConvTxt Set Text2B = :Text; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM