Postgres:将 TEXT 转换为字符串或 'abc' 与 'a' 之间的差异 || 'bc'
Postgres: Convert TEXT to string or difference between 'abc' vs. 'a' || 'bc'
我不明白 Postgres 中字符串和文本的区别。我想使用此函数
从 table 中获取估计的行数
SELECT reltuples::bigint AS estimate
FROM pg_class
WHERE oid = to_regclass('schema.tablename');
这有效,但是会从其他可能不知道当前架构的代码调用 - 所以我想使用 current_schema()
函数。
如果我将代码更改为
SELECT reltuples::bigint AS estimate
FROM pg_class
WHERE oid = to_regclass(current_schema() || '.tablename');
我收到以下错误:
[42883] ERROR: function to_regclass(text) does not exist Hinweis: No function matches the given name and argument types. You might need to add explicit type casts. Position: 67
即使我尝试使用 to_regclass('schema' || '.transactions');
我也会得到同样的错误。
如何转换“||”的结果字符串? CAST(... TO string) 没有 "string" 数据类型,'schema.tablename'
和 'schema' || '.tablename'
有什么区别?
借助 freenode 上的 postgresql 聊天找到解决方案:
SELECT reltuples::bigint AS estimate
FROM pg_class
WHERE oid = to_regclass((current_schema() || '.table')::cstring);
我不明白 Postgres 中字符串和文本的区别。我想使用此函数
从 table 中获取估计的行数 SELECT reltuples::bigint AS estimate
FROM pg_class
WHERE oid = to_regclass('schema.tablename');
这有效,但是会从其他可能不知道当前架构的代码调用 - 所以我想使用 current_schema()
函数。
如果我将代码更改为
SELECT reltuples::bigint AS estimate
FROM pg_class
WHERE oid = to_regclass(current_schema() || '.tablename');
我收到以下错误:
[42883] ERROR: function to_regclass(text) does not exist Hinweis: No function matches the given name and argument types. You might need to add explicit type casts. Position: 67
即使我尝试使用 to_regclass('schema' || '.transactions');
我也会得到同样的错误。
如何转换“||”的结果字符串? CAST(... TO string) 没有 "string" 数据类型,'schema.tablename'
和 'schema' || '.tablename'
有什么区别?
借助 freenode 上的 postgresql 聊天找到解决方案:
SELECT reltuples::bigint AS estimate
FROM pg_class
WHERE oid = to_regclass((current_schema() || '.table')::cstring);