<p dir="auto"><strong>Migrated issue, originally created by Craig Radcliffe (<a href="https://github.com/chradcliffe">@chradcliffe</a>)</strong></p>
<p dir="auto">I noticed this issue while trying to use sqlalchemy as a query generator to produce a string for input into a system that doesn't accept bind parameters.</p>
<p dir="auto">The literal_binds parameter, when passed through the compile_kwargs parameter in the compile() method, does not seem to apply to a LIMIT clause. Here is a basic example:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="from sqlalchemy import select
from sqlalchemy.sql import table, literal_column


t = table(&quot;foo&quot;)
t.append_column(literal_column(&quot;bar&quot;))
print select([t.c[&quot;bar&quot;]]).limit(100).compile(compile_kwargs={'literal_binds': True})
"><pre class="notranslate"><code class="notranslate">from sqlalchemy import select
from sqlalchemy.sql import table, literal_column


t = table("foo")
t.append_column(literal_column("bar"))
print select([t.c["bar"]]).limit(100).compile(compile_kwargs={'literal_binds': True})

</code></pre></div>
<p dir="auto">The output is:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="#!
SELECT foo.bar 
FROM foo
 LIMIT :param_1
"><pre class="notranslate"><code class="notranslate">#!
SELECT foo.bar 
FROM foo
 LIMIT :param_1

</code></pre></div>
<p dir="auto">I would expect:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="#!

SELECT foo.bar 
FROM foo
 LIMIT 100"><pre class="notranslate"><code class="notranslate">#!

SELECT foo.bar 
FROM foo
 LIMIT 100
</code></pre></div>
<p dir="auto">Other clauses, including JOINs, WHEREs, GROUP BYs, etc. work as expected.</p>
<p dir="auto">The issue is present in 0.9.8.</p>
<p dir="auto"><strong>Migrated issue, originally created by Dobes Vandermeer (<a href="https://github.com/dobesv">@dobesv</a>)</strong></p>
<p dir="auto">I am trying to use the "baked query" pattern to reduce the time spent generating SQL, which currently is quite significant for our app.</p>
<p dir="auto">One thing I can't seem to parameterize using a bindparam, however, is the limit on the query.</p>
<p dir="auto">Although in <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="384612604" data-permission-text="Title is private" data-url="https://github.com/sqlalchemy/sqlalchemy/issues/805" data-hovercard-type="issue" data-hovercard-url="/sqlalchemy/sqlalchemy/issues/805/hovercard" href="https://github.com/sqlalchemy/sqlalchemy/issues/805">#805</a> the limit was changed into a bindparam, this doesn't allow me to provide my own bindparam but rather always creates its own bindparam, assuming the limit I procide is a number already.</p>
<p dir="auto">I think that if the places where currently it wraps a numeric limit into a bindparam using sql.literal() it would also check if limit was already a bindparam then it would be good.</p>
<p dir="auto">Here's an example that can be pasted into the test suite:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="    def test_select_with_bindparam_limit(self):
        &quot;&quot;&quot;Does a query allow bindparam for the limit?&quot;&quot;&quot;
        sess = create_session()
        users = []

        q1 = sess.query(self.classes.User).order_by(self.classes.User.id).limit(sa.bindparam('n'))
        for n in xrange(1,4):
            users[:] = q1.params(n=n).all()
            assert len(users) == n"><pre class="notranslate"><code class="notranslate">    def test_select_with_bindparam_limit(self):
        """Does a query allow bindparam for the limit?"""
        sess = create_session()
        users = []

        q1 = sess.query(self.classes.User).order_by(self.classes.User.id).limit(sa.bindparam('n'))
        for n in xrange(1,4):
            users[:] = q1.params(n=n).all()
            assert len(users) == n
</code></pre></div>