Problem parameterizing SQL window fuction

55 views
Skip to first unread message

TheDavidFactor

unread,
Jul 19, 2011, 2:33:07 PM7/19/11
to pyodbc
code:
record = 1
statement = """SELECT * FROM (
SELECT TOP 1 * FROM (
SELECT TOP ? *
FROM Inventory
ORDER BY ID ASC
) AS FOO ORDER BY ID DESC
) AS BAR ORDER BY ID ASC"""
print statement
rows = self.cursor.execute(statement, record).fetchone()

error:
pyodbc.ProgrammingError: ('42000', "[42000] [Microsoft][SQL Server
Native Client 10.0][SQL Server]Incorrect syntax near '@P1'. (102)
(SQLExecDirectW); [42000] [Microsoft][SQL Server Native Client 10.0]
[SQL Server]Statement(s) could not be prepared. (8180)")

I've googled extensively and haven't found anything, that makes think
that either a) I'm doing something stupid (more likely) or b) I'm
trying to do something that's not very common (less likely)

Can anyone point me in the right direction? Thanks!

ashaner

unread,
Jul 19, 2011, 4:02:56 PM7/19/11
to pyodbc
Regarding the use of a parameter for the "top ?" portion I believe
you're c) doing something invalid. Why not just use python formatting
("...top %i..." % record)
instead?
Reply all
Reply to author
Forward
0 new messages