Sql 从表中检索数据时的占位符
我有一个包含公共主题文本的表,需要在运行时插入一些字符串。例如:Sql 从表中检索数据时的占位符,sql,postgresql,placeholder,Sql,Postgresql,Placeholder,我有一个包含公共主题文本的表,需要在运行时插入一些字符串。例如: CREATE TABLE certification.cert_email_dtls ( cert_eid_id numeric(10,0), cert_eid_email_body character varying(8000), ); insert into certification.cert_email_dtls(1,'hello world <blank-value1>); insert into ce
CREATE TABLE certification.cert_email_dtls (
cert_eid_id numeric(10,0),
cert_eid_email_body character varying(8000),
);
insert into certification.cert_email_dtls(1,'hello world <blank-value1>);
insert into certification.cert_email_dtls(2,'hello guys <blank-other-value2>);
insert into certification.cert_email_dtls(3,'hello <blank-value3> india <some-other-value4>);
我的要求是插入一些介于两者之间的值,并生成最终的电子邮件正文。可能是实现这一点的完美工具。请阅读手册中的详细信息
如果您可以以兼容格式保存cert\u eid\u email\u body
,如:
CREATE TABLE cert_email_dtls (
cert_eid_id serial PRIMARY KEY
, cert_eid_email_body text
);
INSERT INTO cert_email_dtls (cert_eid_email_body)
VALUES ('hello %1$s india %2$s');
结果:
cert_eid_email_body
-------------------
hello foo india bar
请注意,第三个参数'baz'
被静默忽略,因为它没有在字符串中引用
旁白:不要用于ID或主键。通常为您提供更好的服务。一方面,PostgreSQL有
replace()
和regex\u replace()
。另一方面,是否有令人信服的理由使用SQL而不是应用程序代码来执行此操作?有多少不同的占位符?它们是唯一的还是可以在多行中弹出?您是一次检索一行,还是一次检索多行?一行中可以有多少占位符?这应该是服务器端函数中的SQL或PL/pgSQL代码吗?和往常一样,请给我你的博士后版本。
SELECT format(cert_eid_email_body, 'foo', 'bar', 'baz') AS cert_eid_email_body
FROM cert_email_dtls
WHERE cert_eid_id = 1;
cert_eid_email_body
-------------------
hello foo india bar