使用运算符“||"的字符串连接或 format() 函数 [英] String concatenation using operator "||" or format() function

查看:68
本文介绍了使用运算符“||"的字符串连接或 format() 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们应该在触发器函数中使用连接运算符 || 还是 format() 函数?

Should we rather use the concatenation operator || or the format() function in trigger functions?

使用一种比另一种有什么优势还是归结为个人喜好和可读性?

Is there any advantage of using one over the other or does it come down to personal preference and readability?

您会说对于简单的连接,使用运算符,而对于更复杂的连接,使用格式函数吗?

Would you say for simple concatenation, use the operator but for more complex concatenation, using the format function?

推荐答案

连接字符串的标准工具基本上有 4 种.最简单/最便宜的第一:

There are basically 4 standard tool for concatenating strings. Simplest / cheapest first:

连接运算符 || ...

  • 如果任何操作数为NULL,则返回NULL.(可能需要也可能不需要.)
  • format()concat() 快一点.
  • 允许连接极少数字符串的最短语法.
  • 对输入类型更加挑剔,因为有多个不同的 || 运算符,并且输入类型需要明确以进行运算符类型解析.
  • 连接字符串类型是 IMMUTABLE,这允许它们在索引或其他不可变波动的地方安全使用是必需的.
  • returns NULL if any operand is NULL. (May or may not be desirable.)
  • is a bit faster than format() or concat().
  • allows shortest syntax for very few strings to concatenate.
  • is more picky about input types as there are multiple different || operators, and the input types need to be unambiguous for operator type resolution.
  • concatenating string-types is IMMUTABLE, which allows their safe use in indexes or other places where immutable volatility is required.

concat() ...

    如果一个参数为 NULL,
  • 不会 返回 NULL.(可能需要也可能不需要.)
  • 对输入类型不那么挑剔,因为所有输入都被强制转换为 text.
  • 允许连接多个字符串的最短语法.
  • 只有函数易变性STABLE(因为它需要any"输入类型并将输入强制转换为文本,其中一些转换取决于时间的语言环境-相关设置).所以不适合需要不可变波动性的地方.看:
    • does not return NULL if one argument is NULL. (May or may not be desirable.)
    • is less picky about input types as all input is coerced to text.
    • allows shortest syntax for more than a couple of strings to concatenate.
    • has only function volatility STABLE (because it takes "any" input type and coerces the input to text, and some of these conversions depend on locale of time-related settings). So not suitable where immutable volatility is required. See:
      • CONCAT used in INDEX causes ERROR: functions in index expression must be marked IMMUTABLE

      concat_ws()(带分隔符")...

      • 使用分隔符连接字符串时允许使用最短的语法.
      • 只为非空字符串插入一个分隔符,大大简化了特定(常见)情况.
      • 其他方面类似于 concat().

      format() ...

      • 在连接变量和常量时允许使用可读的短代码.
      • 提供格式说明符以安全方便地引用字符串和标识符(以防止 SQL 注入和语法错误),使其成为动态 SQL 的首选.(您提到了触发函数,其中使用了大量动态 SQL.)
      • 是最复杂的工具.您可以多次重复使用相同的输入(不同的引用使用不同的格式说明符).
      • 如果任何输入参数为 NULL,
      • 也不会返回 NULL.(可能需要也可能不需要.)
      • 也只有波动性STABLE.
      • allows for readable, short code when concatenating variables and constants.
      • provides format specifiers to safely and conveniently quote stings and identifiers (to defend against SQL injection and syntax errors), making it the first choice for dynamic SQL. (You mention trigger functions, where a lot of dynamic SQL is used.)
      • is the most sophisticated tool. You can reuse the same input multiple times (with different quotation using different format specifiers).
      • does also not return NULL if any of the input parameters are NULL. (May or may not be desirable.)
      • also has only volatility STABLE.

      进一步阅读:

      这篇关于使用运算符“||"的字符串连接或 format() 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆