<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: SQL Statement Problems When Transferring My Code to Arc as a Script in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/sql-statement-problems-when-transferring-my-code/m-p/346760#M27188</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt; I'm trying to make a tool for end users to use in Arc, and I want to make the SQL statement a variable. So far I have this:&amp;nbsp; &lt;BR /&gt; &lt;BR /&gt; &lt;PRE class="lia-code-sample line-numbers language-none"&gt;
Where_Clause= """"GRIDCODE"=6"""
yourvar = 6
&lt;SPAN style="color:&amp;quot;#FF0000&amp;quot;;"&gt;
Where_Clause= arcpy.AddFieldDelimiters(Quakesim_lyr,"GRIDCODE") + " = â??" + yourvar + "â??"&amp;nbsp; # this if GRIDCODE is actually string


Where_Clause= arcpy.AddFieldDelimiters(Quakesim_lyr,"GRIDCODE") + " = "&amp;nbsp; yourvar&amp;nbsp; ## this if GRIDCODE is numeric&lt;/SPAN&gt;


arcpy.SelectLayerByAttribute_management ("Quakesim_lyr", "NEW_SELECTION", Where_Clause)
&lt;/PRE&gt; &lt;BR /&gt; &lt;BR /&gt;which works fine in Python, but when I run it as a tool in arc, I'm getting Invalid Statement errors, even though the statement is the same.&amp;nbsp;&amp;nbsp; &lt;BR /&gt; &lt;BR /&gt;Any advice? Bonus points if you can help me with the next step, which would be to replace the '6' with a variable that I could change.&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Could try something like above to code your where clause using a variable. though thanos_cp78's is more elegant. Guess I need to learn how to use the .format stuff....&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;R_&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 16:20:17 GMT</pubDate>
    <dc:creator>RhettZufelt</dc:creator>
    <dc:date>2021-12-11T16:20:17Z</dc:date>
    <item>
      <title>SQL Statement Problems When Transferring My Code to Arc as a Script</title>
      <link>https://community.esri.com/t5/python-questions/sql-statement-problems-when-transferring-my-code/m-p/346756#M27184</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I'm trying to make a tool for end users to use in Arc, and I want to make the SQL statement a variable. So far I have this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;Where_Clause= """"GRIDCODE"=6""" arcpy.SelectLayerByAttribute_management ("Quakesim_lyr", "NEW_SELECTION", Where_Clause)&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;which works fine in Python, but when I run it as a tool in arc, I'm getting Invalid Statement errors, even though the statement is the same. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Any advice? Bonus points if you can help me with the next step, which would be to replace the '6' with a variable that I could change.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 01 Jul 2013 19:08:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/sql-statement-problems-when-transferring-my-code/m-p/346756#M27184</guid>
      <dc:creator>ScottBarron</dc:creator>
      <dc:date>2013-07-01T19:08:07Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Statement Problems When Transferring My Code to Arc as a Script</title>
      <link>https://community.esri.com/t5/python-questions/sql-statement-problems-when-transferring-my-code/m-p/346757#M27185</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;try,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
inputVariable =&amp;nbsp; arcpy.GetParameterAsText(0)
arcpy.SelectLayerByAttribute_management ("Quakesim_lyr", "NEW_SELECTION", where_clause='"GRIDCODE"={0}'.format(inputVariable))
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 16:20:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/sql-statement-problems-when-transferring-my-code/m-p/346757#M27185</guid>
      <dc:creator>ChrisPedrezuela</dc:creator>
      <dc:date>2021-12-11T16:20:14Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Statement Problems When Transferring My Code to Arc as a Script</title>
      <link>https://community.esri.com/t5/python-questions/sql-statement-problems-when-transferring-my-code/m-p/346758#M27186</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Of course, it was even more simple than that. The reason it wasn't working is because I chose "String" for my input instead of "SQL statement" Once I changed the parameter it worked perfectly.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks anyway!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 01 Jul 2013 19:48:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/sql-statement-problems-when-transferring-my-code/m-p/346758#M27186</guid>
      <dc:creator>ScottBarron</dc:creator>
      <dc:date>2013-07-01T19:48:45Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Statement Problems When Transferring My Code to Arc as a Script</title>
      <link>https://community.esri.com/t5/python-questions/sql-statement-problems-when-transferring-my-code/m-p/346759#M27187</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Yup your right. The difference of the code I put above is that you can make an input variable part of your where_clause which you said was your next step.:o&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Of course, it was even more simple than that. The reason it wasn't working is because I chose "String" for my input instead of "SQL statement" Once I changed the parameter it worked perfectly.&lt;BR /&gt;Thanks anyway!&lt;/BLOCKQUOTE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 01 Jul 2013 20:01:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/sql-statement-problems-when-transferring-my-code/m-p/346759#M27187</guid>
      <dc:creator>ChrisPedrezuela</dc:creator>
      <dc:date>2013-07-01T20:01:54Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Statement Problems When Transferring My Code to Arc as a Script</title>
      <link>https://community.esri.com/t5/python-questions/sql-statement-problems-when-transferring-my-code/m-p/346760#M27188</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt; I'm trying to make a tool for end users to use in Arc, and I want to make the SQL statement a variable. So far I have this:&amp;nbsp; &lt;BR /&gt; &lt;BR /&gt; &lt;PRE class="lia-code-sample line-numbers language-none"&gt;
Where_Clause= """"GRIDCODE"=6"""
yourvar = 6
&lt;SPAN style="color:&amp;quot;#FF0000&amp;quot;;"&gt;
Where_Clause= arcpy.AddFieldDelimiters(Quakesim_lyr,"GRIDCODE") + " = â??" + yourvar + "â??"&amp;nbsp; # this if GRIDCODE is actually string


Where_Clause= arcpy.AddFieldDelimiters(Quakesim_lyr,"GRIDCODE") + " = "&amp;nbsp; yourvar&amp;nbsp; ## this if GRIDCODE is numeric&lt;/SPAN&gt;


arcpy.SelectLayerByAttribute_management ("Quakesim_lyr", "NEW_SELECTION", Where_Clause)
&lt;/PRE&gt; &lt;BR /&gt; &lt;BR /&gt;which works fine in Python, but when I run it as a tool in arc, I'm getting Invalid Statement errors, even though the statement is the same.&amp;nbsp;&amp;nbsp; &lt;BR /&gt; &lt;BR /&gt;Any advice? Bonus points if you can help me with the next step, which would be to replace the '6' with a variable that I could change.&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Could try something like above to code your where clause using a variable. though thanos_cp78's is more elegant. Guess I need to learn how to use the .format stuff....&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;R_&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 16:20:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/sql-statement-problems-when-transferring-my-code/m-p/346760#M27188</guid>
      <dc:creator>RhettZufelt</dc:creator>
      <dc:date>2021-12-11T16:20:17Z</dc:date>
    </item>
  </channel>
</rss>

