title: PostgreSQL Dynamic query to insert a json string(text) to table(text) by 2 param in function
body: <p>I'm having a problem when calling a function with 2 parameters as text to make it a dynamic SQL. The problem is the first param has quote mark ' so my dynamic query is failing. Here is my code: </p>
  
  <pre><code>CREATE OR REPLACE FUNCTION "dbo"."sp_insertbulk"("tablename" text, "jsonitem" text)
  RETURNS SETOF "pg_catalog"."void" AS $BODY$
  BEGIN
      EXECUTE ('INSERT INTO '|| $1 ||' SELECT * FROM json_populate_recordset(null::'|| $1 ||','''|| $2 ||'''::json)');
  END;
  $BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100
  ROWS 1000;
  </code></pre>
  
  <p>I also try using Format():</p>
  
  <pre><code>  EXECUTE FORMAT('INSERT INTO %I SELECT * FROM json_populate_recordset(null::%I , %I::json)',$1,$1,$2);
  </code></pre>
  
  <p>but it will make my jsonitem param unreadable </p>
  
  <pre><code> ({""ShortName"":""abc"",""Name"":""abc"",""Slogan"":""abc"" )
  </code></pre>