<div dir="ltr"><div>Hi,
Felix,</div><div><br></div><div>Thank you very much.</div><div><br></div><div>I worked out
CREATE TABLE AS now. It is rather confusing that syntax can vary between psql, pgadmin and Do loop.</div><div><br></div><div>I am doing a rush job to support combating Coronavirus.</div><div><br></div><div>Regards,</div><div><br></div><div>Shao</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, 26 Mar 2020 at 19:42, Felix Kunde <<a href="mailto:felix-kunde@gmx.de">felix-kunde@gmx.de</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div><div style="font-family:Verdana;font-size:12px"><div>Hi,</div>
<div> </div>
<div>more of a PostgreSQL question, but anyway.</div>
<div> </div>
<div>Within the PL/pgSQL language SELECT INTO behaves differently. It does not work as CREATE TABLE AS, but can be used to store query results into variables.</div>
<div>To dynamically create tables you have to use CREATE TABLE AS instead (I think). Note, when using format better use %I for tables.</div>
<div> </div>
<div>But why create new tables anyway? Write a proper function (instead of using DO) and return a set of records.</div>
<div>Can recommend the PostgreSQL Server Programming book, if you want to dive deeper into PL/pgSQL coding.</div>
<div> </div>
<div>lg</div>
<div>Felix</div>
<div>
<div>
<div name="quote" style="margin:10px 5px 5px 10px;padding:10px 0px 10px 10px;border-left:2px solid rgb(195,217,229)">
<div style="margin:0px 0px 10px"><b>Gesendet:</b> Donnerstag, 26. März 2020 um 14:51 Uhr<br>
<b>Von:</b> "Shaozhong SHI" <<a href="mailto:shishaozhong@gmail.com" target="_blank">shishaozhong@gmail.com</a>><br>
<b>An:</b> "PostGIS Users Discussion" <<a href="mailto:postgis-users@lists.osgeo.org" target="_blank">postgis-users@lists.osgeo.org</a>><br>
<b>Betreff:</b> [postgis-users] Execute format select into</div>
<div name="quoted-content">
<div>Dear All,
<div> </div>
<div>I am trying to use do loop to batch process some data. See the code below:</div>
<div> </div>
Do $$<br>
<br>
DECLARE<br>
schema_name text := 'os_postcodes';<br>
table_name text := 'code_point_britain_point';<br>
tbo text :='os_postcodes.code_point_britain_point';<br>
tb text :='code_point';<br>
overlay_polygons text := 'admin_boundaries.jsna_warwickshire_polygon';<br>
---max = select count(*) from overlay_polygons;<br>
Counter integer :=0;<br>
<br>
BEGIN<br>
For counter IN 1..3 LOOP<br>
<br>
execute format('select a.* into %s from %s a where st_intersects(a.geom, (select b.geom from %s b where mi_prinx = %s))=true;', tb, tbo, overlay_polygons,counter, tb);<br>
<br>
counter :=counter + 1;<br>
RAISE NOTICE 'Counter: %', counter;<br>
END LOOP;<br>
END; $$
<div> </div>
<div> </div>
<div>My question is how to loop "select into a_table from" to create a series of new tables.</div>
<div> </div>
<div>Can anyone help?</div>
<div> </div>
<div>Regards,</div>
<div> </div>
<div>Shao</div>
</div>
_______________________________________________ postgis-users mailing list <a href="mailto:postgis-users@lists.osgeo.org" target="_blank">postgis-users@lists.osgeo.org</a> <a href="https://lists.osgeo.org/mailman/listinfo/postgis-users" target="_blank">https://lists.osgeo.org/mailman/listinfo/postgis-users</a></div>
</div>
</div>
</div></div></div>
_______________________________________________<br>
postgis-users mailing list<br>
<a href="mailto:postgis-users@lists.osgeo.org" target="_blank">postgis-users@lists.osgeo.org</a><br>
<a href="https://lists.osgeo.org/mailman/listinfo/postgis-users" rel="noreferrer" target="_blank">https://lists.osgeo.org/mailman/listinfo/postgis-users</a></blockquote></div></div>