Эта страница переведена машинным переводом. Читайте английский оригинал. English

Библиотека IBSurgeon

Как поместить 29 символов в VARCHAR(10)

Firebird Friday Joke #6 (из https://t.me/firebirdsql)

Старая, но всё ещё хорошая шутка: с многобайтовыми наборами символов можно вставить больше символов, чем заявлено. Учите Unicode! :)

Наслаждайтесь!

Code
C:\HQbird\Firebird25.in>isql
Use CONNECT or CREATE DATABASE to specify a database
SQL> create database "c:\databases\test1.fdb" user "SYSDBA" password "masterkey";
SQL> create table t2(v1 varchar(10) character set UNICODE_FSS);
SQL> insert into t2(v1) values('12345678901');
SQL> insert into t2(v1) values('123456789012');
SQL> insert into t2(v1) values('1234567890123');
SQL> insert into t2(v1) values('12345678901234');
SQL> insert into t2(v1) values('123456789012345');
SQL> insert into t2(v1) values('1234567890123456');
SQL> insert into t2(v1) values('12345678901234567');
SQL> insert into t2(v1) values('1234567890123456789101234');
SQL> insert into t2(v1) values('12345678901234567891012345');
SQL> insert into t2(v1) values('123456789012345678910123456');
SQL> insert into t2(v1) values('1234567890123456789101234567');
SQL> insert into t2(v1) values('12345678901234567891012345678');
SQL> insert into t2(v1) values('123456789012345678910123456789');  <--- 29 символов!
SQL> insert into t2(v1) values('1234567890123456789101234567890');
Statement failed, SQLSTATE = 22001
arithmetic exception, numeric overflow, or string truncation
-string right truncation
SQL> create table t3(v1 varchar(10) character set UTF8);
SQL> insert into t2(v1) values('1234567890');
SQL> insert into t2(v1) values('12345678901');
SQL> insert into t2(v1) values('123456789012');
SQL> insert into t2(v1) values('123456789012345678');
SQL> insert into t2(v1) values('1234567890123456789');
SQL> insert into t2(v1) values('12345678901234567890');
SQL> insert into t2(v1) values('123456789012345678901');
SQL> insert into t2(v1) values('12345678901234567890123');
SQL> insert into t2(v1) values('1234567890123456789012345678');
SQL> insert into t2(v1) values('123456789012345678901234567890');
SQL> insert into t2(v1) values('1234567890123456789012345678901');
Statement failed, SQLSTATE = 22001
arithmetic exception, numeric overflow, or string truncation
-string right truncation
SQL>

В 2.5 это работает, в 3.0 ведёт себя иначе с utf8

Code
SQL> create table t2(v1 varchar(10) character set UNICODE_FSS);
SQL> insert into t2(v1) values('12345678901');
SQL> insert into t2(v1) values('12345678901234567890');
SQL> insert into t2(v1) values('12345678901234567890123456789');
SQL> insert into t2(v1) values('123456789012345678901234567890');
SQL> insert into t2(v1) values('1234567890123456789012345678901');
Statement failed, SQLSTATE = 22001
arithmetic exception, numeric overflow, or string truncation
-string right truncation
-expected length 10, actual 30
SQL> create table t3(v1 varchar(10) character set utf8);
SQL> insert into t3(v1) values('1234567890');
SQL> insert into t3(v1) values('12345678901');
Statement failed, SQLSTATE = 22001
arithmetic exception, numeric overflow, or string truncation
-string right truncation
-expected length 10, actual 11
SQL>