首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在MySQL列注释中连接字符串?

如何在MySQL列注释中连接字符串?
EN

Stack Overflow用户
提问于 2019-08-09 07:11:43
回答 1查看 54关注 0票数 0

我正在尝试添加一个没有换行符的长数据库注释,同时在我的SQL输入文件中有一个合理的行长。

考虑下面的代码片段:

代码语言:javascript
复制
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,但同样不起作用。我正在努力实现与以下目标相当的目标。

代码语言:javascript
复制
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注释字符串中连接字符串吗?

EN

回答 1

Stack Overflow用户

发布于 2020-11-04 18:13:20

据我所知,在MySQL中,你没有像"+“这样的操作符来连接字符串。concat()是这里的基本方法。

我也遇到过同样的问题,我使用这个存储过程通过DDL解决了这个限制。这段代码实际上将注释作为一个整体添加到表中,而您询问的是如何向单个字段添加注释。我希望这个例子对适应你的需求有足够的帮助。最后,这段代码充其量是“原型”质量。它对我来说很好(实际上还没怎么用过),但对你可能不起作用:

代码语言:javascript
复制
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;

END
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57421711

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档