--- /dev/null
+.. change::
+ :tags: bug, mssql
+ :tickets: 4587
+
+ Fixed issue in SQL Server dialect where if a bound parameter were present in
+ an ORDER BY expression that would ultimately not be rendered in the SQL
+ Server version of the statement, the parameters would still be part of the
+ execution parameters, leading to DBAPI-level errors. Pull request courtesy
+ Matt Lewellyn.
return ""
def order_by_clause(self, select, **kw):
+ # MSSQL only allows ORDER BY in subqueries if there is a LIMIT
+ if self.is_subquery() and not select._limit:
+ # avoid processing the order by clause if we won't end up
+ # using it, because we don't want all the bind params tacked
+ # onto the positional list if that is what the dbapi requires
+ return ""
+
order_by = self.process(select._order_by_clause, **kw)
- # MSSQL only allows ORDER BY in subqueries if there is a LIMIT
- if order_by and (not self.is_subquery() or select._limit):
+ if order_by:
return " ORDER BY " + order_by
else:
return ""
"foo.myid = mytable.myid",
)
+ def test_noorderby_parameters_insubquery(self):
+ """test that the ms-sql dialect does not include ORDER BY
+ positional parameters in subqueries"""
+
+ table1 = table(
+ "mytable",
+ column("myid", Integer),
+ column("name", String),
+ column("description", String),
+ )
+
+ q = select(
+ [table1.c.myid, sql.literal('bar').label('c1')],
+ order_by=[table1.c.name + '-']
+ ).alias("foo")
+ crit = q.c.myid == table1.c.myid
+ dialect = mssql.dialect()
+ dialect.paramstyle = "qmark"
+ dialect.positional = True
+ self.assert_compile(
+ select(["*"], crit),
+ "SELECT * FROM (SELECT mytable.myid AS "
+ "myid, ? AS c1 FROM mytable) AS foo, mytable WHERE "
+ "foo.myid = mytable.myid",
+ dialect=dialect,
+ checkparams={'param_1': 'bar'},
+ # if name_1 is included, too many parameters are passed to dbapi
+ checkpositional=('bar', )
+ )
+
def test_force_schema_quoted_name_w_dot_case_insensitive(self):
metadata = MetaData()
tbl = Table(