NAME_CONST()

NAME_CONST(name, value) returns the given value with the specified column name. It is used primarily for replication scenarios and constructing result sets with fixed column names. The name argument must be a constant string literal, and the value must be a constant expression.

Description

NAME_CONST(name, value) returns the value argument and assigns the column name name to the result. This function is typically used internally for replication purposes, where the replication slave needs to reconstruct a result set with specific column names.

  • The name argument must be a constant string literal or a numeric literal. Expressions, column references, and NULL are not allowed as the name.

  • The value argument must be a constant value: integers, strings, decimals, floating-point numbers, and NULL are supported. Expressions, function calls (such as NOW(), ABS()), and CAST() calls are not supported as the value.

  • The function can be used in subqueries and as a column alias source.

Syntax

> NAME_CONST(name, value)

Arguments

Arguments

Description

name

Required. A constant string literal or numeric literal specifying the column name for the result.

value

Required. A constant value (integer, string, decimal, float, or NULL) to be returned. Expressions and function calls are not supported.

Examples

DROP DATABASE IF EXISTS test_name_const;
CREATE DATABASE test_name_const;
USE test_name_const;

-- Basic usage with integer value
SELECT NAME_CONST('myname', 14);

-- String value
SELECT NAME_CONST('str_name', 'abc');

-- NULL value
SELECT NAME_CONST('null_name', NULL);

-- Decimal value
SELECT NAME_CONST('decimal_name', 12.34);

-- Negative decimal
SELECT NAME_CONST('neg_decimal_name', -12.34);

-- Scientific notation float
SELECT NAME_CONST('sci_float_name', 1.5e0);

-- Using as a column alias
SELECT NAME_CONST('myname', 14) AS alias_name;

-- In a subquery
SELECT t.myname FROM (SELECT NAME_CONST('myname', 42)) t;

-- Multiple columns via NAME_CONST in a subquery
SELECT * FROM (SELECT NAME_CONST('v', 'hello'), NAME_CONST('n', 99)) sub;

-- Collated string value
SELECT NAME_CONST('collate_str_name', _utf8mb4'test' COLLATE utf8mb4_unicode_ci);

DROP DATABASE test_name_const;

Notes

  • In MatrixOne, the name argument must be a constant literal. Unlike MySQL, where expressions may be folded in some contexts, MatrixOne requires an explicit constant string or numeric literal.

  • The value argument also must be a constant. Expressions such as ABS(-1), function calls like NOW(), and type casts like CAST(14 AS SIGNED) are not supported and will produce an error.

  • This function is primarily intended for internal use in replication. Most application-level queries do not need to use it directly.

  • NAME_CONST() can be used with subqueries to control column naming in derived tables.