【发布时间】:2020-12-03 09:07:45
【问题描述】:
我正在编写 sql 脚本,只有当表中缺少记录时,我才需要在数据库中插入记录,如果表中确实存在记录,我需要更新 template 这是我的 sql 查询
insert into deployment.nodes values('sind','11', now(),'temp',
'<table id="producttable">
<thead>
<tr>
<td>UPC_Code</td>
<td>Product_Name</td>
</tr>
</thead>
<tbody>
<!-- existing data could optionally be included here -->
</tbody>
</table>
<template id="productrow">
<tr>
<td class="record"></td>
<td></td>
</tr>
</template> ') on conflict on constraint nodes_pkey
DO UPDATE SET latest=now(), status='active',
agent='<table id="producttable">
<thead>
<tr>
<td>UPC_Code</td>
<td>Product_Name</td>
</tr>
</thead>
<tbody>
<!-- existing data could optionally be included here -->
</tbody>
</table>
<template id="productrow">
<tr>
<td class="record"></td>
<td></td>
</tr>
</template>'
这个 sql 脚本对我来说运行良好,但我想在变量中使用 HTML 的东西来最小化 sql 脚本的长度。
让我们说
var template = '<html> .... </html>' and then query will be like
insert into deployment.nodes values('sind','11', now(),'temp',template) on conflict on constraint
nodes_pkey DO UPDATE SET latest=now(), status='active',agent=template
【问题讨论】:
-
你可以把它作为返回值存储在一个函数中吗?
-
@VynlJunkie 任何提示如何获得这些?
-
set agent = EXCLUDED.agent见这里:postgresql.org/docs/current/sql-insert.html -
@MikeOrganek 无法从墨迹中理解,请您举个小例子
标签: sql postgresql sql-insert