0

I am working on sql script in which i need to insert record in the db only if record is missing in the table , if record do exists in the table i need to update the template Here is my sql query

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>'

This sql script is working fine for me but i want to use HTML stuff in a variable to minimize the length of sql script.

lets say

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
4
  • you can store it in a function as the return value? Commented Dec 3, 2020 at 9:13
  • @VynlJunkie any hint how do get those ? Commented Dec 3, 2020 at 9:18
  • set agent = EXCLUDED.agent See here: postgresql.org/docs/current/sql-insert.html Commented Dec 3, 2020 at 9:32
  • @MikeOrganek couldn't understand from the ink could you please give a small example Commented Dec 3, 2020 at 9:37

1 Answer 1

3

Example of using the EXCLUDED pseudotable:

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 = EXCLUDED.latest, 
      status = EXCLUDED.status,
      agent = EXCLUDED.agent
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.