Note:
This topic has been translated from a Chinese forum by GPT and might contain errors.Original topic: 关于预处理语句不设置别名的列的最大数量

【TiDB Version】6.5
When using MyBatis, some parameters in the XML mapper did not specify column aliases, resulting in a
Duplicate column name "NULL"
error. This is due to differences in behavior between TiDB and MySQL. I hope to understand the related restrictions of the two databases and whether it is possible to bypass this error without adding aliases to the columns.
MySQL does not allow preparing the following statement, but TiDB does:
prepare stmt from
'
select tmp.*
from (
select ?, ?
from dual
) tmp
';
However, TiDB does not allow preparing the following statement or using more than two ?
placeholders:
# Compared to the first one, only the "from dual" is removed, and if "from dual" is not written on a new line, it cannot be prepared either
prepare stmt from
'
select tmp.*
from (
select ?, ?
) tmp
';
# Declaring three unnamed placeholders
prepare stmt from
'
select tmp.*
from (
select ?, ?, ?
) tmp
';
Both databases return the error message Duplicate column name ‘?’ when reporting errors.
When using Oracle previously, since the bind of prepared statements was provided through :n placeholders, it did not seem to be restricted by not setting aliases for parameters. The following prepared statement can be compiled and executed normally:
declare
sql_stmt varchar2(200);
begin
sql_stmt := '
select tmp.*
from (
select :1, :2, :3
from dual
) tmp
';
execute immediate sql_stmt using 'value1', 'value2';
end;