我正在尝试添加一个没有换行符的长数据库注释,同时在我的SQL输入文件中有一个合理的行长。
考虑下面的代码片段:
CREATE TABLE foo (
bar VARCHAR(63) NOT NULL
COMMENT 'This is the beginning of a very long comment that goes on and on and on. I want this line to be part of the same logical line while having newlines in the input source code.',我想在我的源代码(SQL文件)中中断上面的行,同时在数据库中保持相同的输出。数据库中的输出被定义为SELECT column_comment from information_schema.columns where table_name='foo'的输出。
在许多其他编程语言中使用+进行字符串连接是行不通的。我也尝试过使用CONCAT,但同样不起作用。我正在努力实现与以下目标相当的目标。
CREATE TABLE foo (
bar VARCHAR(63) NOT NULL
COMMENT 'This is the beginning of a very long comment' +
' that goes on and on and on. I want this line' +
' to be part of the same logical line while' +
' having newlines in the input source code.',可以在这些MySQL注释字符串中连接字符串吗?
发布于 2020-11-04 18:13:20
据我所知,在MySQL中,你没有像"+“这样的操作符来连接字符串。concat()是这里的基本方法。
我也遇到过同样的问题,我使用这个存储过程通过DDL解决了这个限制。这段代码实际上将注释作为一个整体添加到表中,而您询问的是如何向单个字段添加注释。我希望这个例子对适应你的需求有足够的帮助。最后,这段代码充其量是“原型”质量。它对我来说很好(实际上还没怎么用过),但对你可能不起作用:
CREATE DEFINER=`strompf`@`localhost` PROCEDURE `add_comment_to_table`(
in_table_name tinytext,
in_comment varchar(1024)
)
BEGIN
#
# Add comment to the given table
################################################################################
#
#
################################################################################
# Create DDL
################################################################################
#
set @ddl=concat
(
'alter table ', in_table_name, ' comment=',
char(34), in_comment, char(34),';'
);
# select @ddl;
################################################################################
# Execute DDL
################################################################################
#
prepare ps1 from @ddl;
execute ps1;
drop prepare ps1;
ENDhttps://stackoverflow.com/questions/57421711
复制相似问题