gpt4 book ai didi

sql - 在 postgresql 断言消息中包含参数

转载 作者:行者123 更新时间:2023-11-29 14:29:39 34 4
gpt4 key购买 nike

有没有办法在 PostgreSQL ASSERT 消息中包含参数。

例子:

do $$ 
declare
c integer;
begin
c := (select count(*) from pg_database);
assert c = 7, 'not 7!';
assert c = 8, 'not 8!';
assert c = 5, ('% not 5!' , c);
end;$$;

这个有效:

assert c = 8, 'not 8!';

这个断言:

assert c = 5, ('% not 5!' , c);

按应有的方式显示错误消息:

SQL Error [P0004]: ERROR: ("% not 5!",7)
Where: PL/pgSQL function inline_code_block line 7 at ASSERT
ERROR: ("% not 5!",7)
Where: PL/pgSQL function inline_code_block line 7 at ASSERT
ERROR: ("% not 5!",7)
Where: PL/pgSQL function inline_code_block line 7 at ASSERT

但变量不会替换文本中的 %。

最佳答案

使用FORMAT :

ASSERT c = 5, FORMAT('%s not 5!', c);

关于sql - 在 postgresql 断言消息中包含参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53616419/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com