<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"><head><meta http-equiv=Content-Type content="text/html; charset=utf-8"><meta name=Generator content="Microsoft Word 15 (filtered medium)"><style><!--
/* Font Definitions */
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
{font-family:Verdana;
panose-1:2 11 6 4 3 5 4 4 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
margin-bottom:.0001pt;
font-size:12.0pt;
font-family:"Times New Roman",serif;}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:blue;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:purple;
text-decoration:underline;}
span.EmailStyle17
{mso-style-type:personal-reply;
font-family:"Calibri",sans-serif;
color:#1F497D;}
.MsoChpDefault
{mso-style-type:export-only;
font-family:"Calibri",sans-serif;}
@page WordSection1
{size:8.5in 11.0in;
margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
{page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]--></head><body lang=EN-US link=blue vlink=purple><div class=WordSection1><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D'>For what it’s worth SELECT * INTO table_to_be_created FROM some_other_table;<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D'>Is valid SQL construct. It’s more supported than CREATE TABLE AS across different database systems.<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D'>I use SELECT * INTO syntax all the time in SQL even in PostgreSQL and it works fine.<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D'>What you are constructing in your loop is an SQL statement so should be able to use INTO table_to_be_created. <o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D'>However I notice your table you are creating is not in the loop so would fail on second run because you keep on trying to create the table.<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D'>Your code should be something like<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:9.0pt;font-family:"Verdana",sans-serif'>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 || counter::text, tbo, overlay_polygons,counter, tb);<br><br>-- counter :=counter + 1; #you don’t need this the IN clause already does this<br>RAISE NOTICE 'Counter: %', counter;<br>END LOOP;<br>END; $$ <o:p></o:p></span></p><p class=MsoNormal><span style='font-size:9.0pt;font-family:"Verdana",sans-serif'> <o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D'><o:p> </o:p></span></p><div style='border:none;border-left:solid blue 1.5pt;padding:0in 0in 0in 4.0pt'><div><div style='border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0in 0in 0in'><p class=MsoNormal><b><span style='font-size:11.0pt;font-family:"Calibri",sans-serif'>From:</span></b><span style='font-size:11.0pt;font-family:"Calibri",sans-serif'> postgis-users [mailto:postgis-users-bounces@lists.osgeo.org] <b>On Behalf Of </b>Shaozhong SHI<br><b>Sent:</b> Thursday, March 26, 2020 7:32 PM<br><b>To:</b> PostGIS Users Discussion <postgis-users@lists.osgeo.org><br><b>Subject:</b> Re: [postgis-users] Execute format select into<o:p></o:p></span></p></div></div><p class=MsoNormal><o:p> </o:p></p><div><div><p class=MsoNormal>Hi, Felix,<o:p></o:p></p></div><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal>Thank you very much.<o:p></o:p></p></div><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal>I worked out CREATE TABLE AS now. It is rather confusing that syntax can vary between psql, pgadmin and Do loop.<o:p></o:p></p></div><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal>I am doing a rush job to support combating Coronavirus.<o:p></o:p></p></div><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal>Regards,<o:p></o:p></p></div><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal>Shao<o:p></o:p></p></div><p class=MsoNormal><o:p> </o:p></p><div><div><p class=MsoNormal>On Thu, 26 Mar 2020 at 19:42, Felix Kunde <<a href="mailto:felix-kunde@gmx.de">felix-kunde@gmx.de</a>> wrote:<o:p></o:p></p></div><blockquote style='border:none;border-left:solid #CCCCCC 1.0pt;padding:0in 0in 0in 6.0pt;margin-left:4.8pt;margin-right:0in'><div><div><div><p class=MsoNormal><span style='font-size:9.0pt;font-family:"Verdana",sans-serif'>Hi,<o:p></o:p></span></p></div><div><p class=MsoNormal><span style='font-size:9.0pt;font-family:"Verdana",sans-serif'> <o:p></o:p></span></p></div><div><p class=MsoNormal><span style='font-size:9.0pt;font-family:"Verdana",sans-serif'>more of a PostgreSQL question, but anyway.<o:p></o:p></span></p></div><div><p class=MsoNormal><span style='font-size:9.0pt;font-family:"Verdana",sans-serif'> <o:p></o:p></span></p></div><div><p class=MsoNormal><span style='font-size:9.0pt;font-family:"Verdana",sans-serif'>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.<o:p></o:p></span></p></div><div><p class=MsoNormal><span style='font-size:9.0pt;font-family:"Verdana",sans-serif'>To dynamically create tables you have to use CREATE TABLE AS instead (I think). Note, when using format better use %I for tables.<o:p></o:p></span></p></div><div><p class=MsoNormal><span style='font-size:9.0pt;font-family:"Verdana",sans-serif'> <o:p></o:p></span></p></div><div><p class=MsoNormal><span style='font-size:9.0pt;font-family:"Verdana",sans-serif'>But why create new tables anyway? Write a proper function (instead of using DO) and return a set of records.<o:p></o:p></span></p></div><div><p class=MsoNormal><span style='font-size:9.0pt;font-family:"Verdana",sans-serif'>Can recommend the PostgreSQL Server Programming book, if you want to dive deeper into PL/pgSQL coding.<o:p></o:p></span></p></div><div><p class=MsoNormal><span style='font-size:9.0pt;font-family:"Verdana",sans-serif'> <o:p></o:p></span></p></div><div><p class=MsoNormal><span style='font-size:9.0pt;font-family:"Verdana",sans-serif'>lg<o:p></o:p></span></p></div><div><p class=MsoNormal><span style='font-size:9.0pt;font-family:"Verdana",sans-serif'>Felix<o:p></o:p></span></p></div><div><p class=MsoNormal><span style='font-size:9.0pt;font-family:"Verdana",sans-serif'> <o:p></o:p></span></p><div><p class=MsoNormal><span style='font-size:9.0pt;font-family:"Verdana",sans-serif'> <o:p></o:p></span></p><div style='border:none;border-left:solid #C3D9E5 1.5pt;padding:0in 0in 0in 8.0pt;margin-left:7.5pt;margin-top:7.5pt;margin-right:3.75pt;margin-bottom:3.75pt' name=quote><div style='margin-bottom:7.5pt'><p class=MsoNormal><b><span style='font-size:9.0pt;font-family:"Verdana",sans-serif'>Gesendet:</span></b><span style='font-size:9.0pt;font-family:"Verdana",sans-serif'> 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<o:p></o:p></span></p></div><div name=quoted-content><div><p class=MsoNormal><span style='font-size:9.0pt;font-family:"Verdana",sans-serif'>Dear All, <o:p></o:p></span></p><div><p class=MsoNormal><span style='font-size:9.0pt;font-family:"Verdana",sans-serif'> <o:p></o:p></span></p></div><div><p class=MsoNormal><span style='font-size:9.0pt;font-family:"Verdana",sans-serif'>I am trying to use do loop to batch process some data. See the code below:<o:p></o:p></span></p></div><div><p class=MsoNormal><span style='font-size:9.0pt;font-family:"Verdana",sans-serif'> <o:p></o:p></span></p></div><p class=MsoNormal><span style='font-size:9.0pt;font-family:"Verdana",sans-serif'>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; $$ <o:p></o:p></span></p><div><p class=MsoNormal><span style='font-size:9.0pt;font-family:"Verdana",sans-serif'> <o:p></o:p></span></p></div><div><p class=MsoNormal><span style='font-size:9.0pt;font-family:"Verdana",sans-serif'> <o:p></o:p></span></p></div><div><p class=MsoNormal><span style='font-size:9.0pt;font-family:"Verdana",sans-serif'>My question is how to loop "select into a_table from" to create a series of new tables.<o:p></o:p></span></p></div><div><p class=MsoNormal><span style='font-size:9.0pt;font-family:"Verdana",sans-serif'> <o:p></o:p></span></p></div><div><p class=MsoNormal><span style='font-size:9.0pt;font-family:"Verdana",sans-serif'>Can anyone help?<o:p></o:p></span></p></div><div><p class=MsoNormal><span style='font-size:9.0pt;font-family:"Verdana",sans-serif'> <o:p></o:p></span></p></div><div><p class=MsoNormal><span style='font-size:9.0pt;font-family:"Verdana",sans-serif'>Regards,<o:p></o:p></span></p></div><div><p class=MsoNormal><span style='font-size:9.0pt;font-family:"Verdana",sans-serif'> <o:p></o:p></span></p></div><div><p class=MsoNormal><span style='font-size:9.0pt;font-family:"Verdana",sans-serif'>Shao<o:p></o:p></span></p></div></div><p class=MsoNormal><span style='font-size:9.0pt;font-family:"Verdana",sans-serif'>_______________________________________________ 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><o:p></o:p></span></p></div></div></div></div></div></div><p class=MsoNormal>_______________________________________________<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" target="_blank">https://lists.osgeo.org/mailman/listinfo/postgis-users</a><o:p></o:p></p></blockquote></div></div></div></div></body></html>