Trying to learn how to use Select_analysis but the queries cause the

ERROR 000358: Invalid expression.

Piece of code:

                 'WACDE'),where_clause="Select test1.KA FROM test1 WHERE test1.NUM = '12'")

What is the correct form?

  • 1
    What is your data source? SQL Server, shapefile, PostgreSQL, File Geodatabase, etc? Your datasource will dictate how you communicate with it via SQL statements. See here. – Barbarossa Oct 22 '18 at 15:01
  • 2
    The where_clause parameter accepts only a WHERE clause (with or without the WHERE). There are a number of examples of correct Select_analysis usage in the documentation. – Vince Oct 22 '18 at 15:10
  • it's a geodatabse in ArcMap. – user130347 Oct 23 '18 at 5:56
  • @Vince So is there any way to accept a normal select statement as we would in a normal database or they always have to be like the answer below? – user130347 Oct 23 '18 at 5:58
  • 1
    If it was an Enterprise database, then you could use arcpy.ArcSDESQLExecute, but file database doesn't have a backend SQL engine. You need to parse the SELECT statement elements and drop them into their respective parameters. – Vince Oct 23 '18 at 10:32
import arcpy

arcpy.env.workspace = r'C:\Users\users\Desktop\05\Project1.gdb'
in_features = 'test1'
out_feature_class = 'WACDE'
where_clause = "NUM = '12'"
arcpy.Select_analysis(in_features , out_feature_class , where_clause)

Check this out, it should work. I assumed your column is named NUM. Let me know if that was your goal to achieve.

  • I know this works the problem is that I have a column full of select statements and I will need to use them as full sql queries like in a database. Can you tell me how to make it work with the query above?I am upvoting for the effort though – user130347 Oct 22 '18 at 14:23
  • Just add some more info about your problem, ex. some screenshots of that table. – JuniorPythonNewbie Oct 22 '18 at 14:27
  • To explain, the select query I have in the question is an alternative way to do what you did.The thing is that the queries have the first form (select ... from .. where). How can I make it process queries in that form, just like in a database?using Select_analysis? Thanks – user130347 Oct 22 '18 at 14:31
  • 2
    Is this a column as an attribute in a shapefile or featureclass, or in a CSV table, or some other data source? When you read the input, you should be able to break it up using split in arcpy, for example whereclause = sqlquery.split("WHERE ")[-1] – smiller Oct 22 '18 at 15:20
  • it's a geodatabse in ArcMap. Is there a way to use a normal select statement as we would in a database? Or it has to be typed like in the answer? – user130347 Oct 23 '18 at 6:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.