ogr2ogr sql query from text file

It is understandable to read only the manual page of ogr2ogr http://www.gdal.org/ogr2ogr.html and miss the page about options which are general to all OGR utilities https://gdal.org/programs/vector_common_options.html because the first one does not mention that the latter exists. However, you can save you -sql "<query>" terms into text files and reuse them by referencing them with --optfile as documented in the common options for all ogrtools.

Contents of file C:/TEMP/sql.sql:

-sql "SELECT * FROM SCHEMA.TABLE"

Ogr2ogr command to use:

ogrinfo -al -so "OCI:USER/PASSWORD@SERVER/DATABASE:SCHEMA.TABLE" --optfile C:/TEMP/sql.sql

Notice also this usage example and not so clear description sentence in the ogr2ogr manual page:

[-sql |@filename]

-sql sql_statement: SQL statement to execute. The resulting table/layer will be saved to the output. Starting with GDAL 2.1, the syntax can be used to indicate that the content is in the pointed filename.

It means that with GDAL 2.1 which is currently the development version you can use also this syntax:

ogrinfo -al -so "OCI:USER/PASSWORD@SERVER/DATABASE:SCHEMA.TABLE" -sql @C:/TEMP/sql.sql

If I understand correctly the command is in the file, so you will need to read that file to get the text.

You can grab the first line of your file with and hold it in the variable sqlcmd using set /p sqlcmd=< sql.sql. And then %sqlcmd% should work in your ogrinfo command.

(I got the command line information from here: https://stackoverflow.com/questions/130116/windows-batch-commands-to-read-first-line-from-text-file.)

Tags:

Ogr

Ogr2Ogr