In my QGIS Plugin I am filtering layers with this Python code:

FeatureIterator = layer.getFeatures( QgsFeatureRequest().setFilterExpression ("Fieldname='SomeValue'") )
for feature in FeatureIterator :
    attrs = feature.attributes()
        print "Feature ID: " + str(feature.id()) + " " + str(attrs)

This works for most layer-types (shapefile, PostGIS, personal GDB,etc). But when I tried it with MSSQL Spatial layers, no results are returned. The iterator is empty.

Edit: It works if I use the same filter clause (Fieldname='SomeValue') as a Feature subset at Layer-Properties. Then the layer shows only the intended subset.

I work with QGIS 2.2 and 2.4. MSSQL Server is 2008R2. My test data was exactly the same in PostGIS and MSSQL, so I know that the filter expression is correct.

Could this be a bug?

Edit: I was able to track the SQL queries that are executed on the Database. Instead of the filter expression that I intended, QGIS adds an Intersect clause and an ID filter to the query:

select .... from ... where 
[shape].STIntersects([geometry]::STGeomFromText('POLYGON((0.00000000 0.00000000, 0.00000000 0.00000000, 0.00000000 0.00000000, 0.00000000 0.00000000, 0.00000000 0.00000000))',25832)) = 1 and [objectid] = 30681189080760422

The Coordinates of the intersect-rectangle are always 0 and the objectid-value doesn't exist.

  • Can you use SQL Server profiler to exactly detetrmine which commands gets issued to the database? Maybe its some weird escaping issue. Does your fieldname have special chars? Try enclosing it in [ ].
    – til_b
    Aug 15, 2014 at 9:58
  • I dont have the profiler. The expression is not complicated: "id='DENIAL0T000002Sy'". I tried with brackets around the fieldname, but it had no effect.
    – steloe
    Aug 15, 2014 at 11:04
  • I ran into problems connecting to tables which had backslashes in their schema names. But since your layer displays allright that seems not to be your issue.
    – til_b
    Aug 15, 2014 at 11:12

1 Answer 1


It was a Bug. I filed it on the qgis Bugtracker and it got fixed. It is included in the 2.5 Version (nightly builds)

see here: https://hub.qgis.org/issues/11076

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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