-1

I'm using pyodbc to connect to a .mdb file and do somethings.

sql = ("""\
  DECLARE @i int  = 1;
  ...somevalid sql...
""")
cursor.execute(sql)

The above gives me the error "Invalid SQL Statement; expected 'Delete',.....

@i is not used anywhere in the sql, and the sql below will run without the declare statement. I have seen in multiple places other people DO do this like here so it is supposed to be possible.

Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
SpeedOfRound
  • 1,210
  • 11
  • 26

1 Answers1

2

You are connecting to an Access database (.mdb) so you need to use the Microsoft Access dialect of SQL. DECLARE is a T-SQL statement (the Microsoft SQL Server dialect of SQL) that is not valid for Access SQL.

Note also that Access SQL only supports the execution of one SQL statement at a time.

Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
  • Oh I see, I haven't really used access before. so I guess something like this is not possible: https://stackoverflow.com/a/13629639/5471957 – SpeedOfRound Nov 16 '18 at 14:02
  • Not in that form (anonymous code block). Try asking a [new question](https://stackoverflow.com/questions/ask) describing what you actually want to accomplish. – Gord Thompson Nov 16 '18 at 15:26