sql - 在 psql CLI 中从任何大小写到较小大小写的变量值转换
问题描述
我正在创建一个脚本来通过 psql CLI 在 Postgres 中创建数据库用户,我面临的挑战是从任何大小写到小写的值转换正在发生或无法找到相同的解决方案。
\prompt 'Please input user name (must be lowercase): ' inputName
\set lowerVal lower(:inputName) --This is not working
按照我的预期,lowerVal 变量应该将 inputValue 转换为小写值。谷歌搜索但没有得到相同的解决方案。
解决方案
psql
不能评估表达式,所以你的例子不能工作。您可以使用 statement\gset
评估查询并将结果存储到psql
变量:
postgres=# \prompt 'Please input user name (must be lowercase): ' inputName
Please input user name (must be lowercase): AHOJ
postgres=# select lower(:'inputName') as "inputName" \gset
postgres=# \echo :inputName
ahoj
可能最好只使用小字符作为psql
变量名。
另一种可能性是使用带有反撇号的 shell 表达式评估:
postgres=# \set str AHOJ
postgres=# \set str `echo ":str" | tr "[:upper:]" "[:lower:]"`
postgres=# \echo :str
ahoj
此示例仅适用于类 Unix 系统。
推荐阅读
- python-3.x - Pandas:导入多个 CSV 文件并追加在一起,使元素按照时间索引排列
- sql - 可以从 PL/SQL Oracle 中的正在进行的游标中删除字段吗?
- constructor - 在 F# 类中,如何在初始化时使用另一个值中的值?
- algorithm - 哪种排序方法最适合对大小为 0 到 2^40 的 100 万个整数进行排序,以便获得最小的波浪号时间复杂度?
- c# - 如何在 c# 中禁用 HeaderText Click?
- r - 假设我不知道数据框中的列名,如何根据该列中的值对 df 进行排序?
- python - PyTorch,按元素应用不同的功能
- python - 如何为文件中的每个单词添加数字组合?
- java - 如何搜索该列表
包含使用流的字符串的给定数组 [] 的所有值? - c++ - 从 boost fusion 适应结构中获取成员类型列表