Passing table name as parameter in pyobbc

I am trying to pass the table name in pyodbc as a parameter for accessing data from MS SQL 2005. I was trying to replace it with ? but it never works. I would be happy to receive any advice on how to achieve this.

+6
source share
2 answers

Since you are using pyodbc, I assume that you are not calling SPROC. I recommend creating your SQL string in python and then passing it SQL to execute.

 import pyodbc dbconn = pyodbc.connect(ConnectionString) c = dbconn.cursor() j = 'table1' #where table1 is entered, retreived, etc query = "Select * from %s" % j c.execute(query) result = c.fetchall() print 'Done' 
+4
source

You cannot have a variable as the name of a SQL Server table. For this you need to use dynamic SQL .

See how to work with dynamic table names. http://www.sommarskog.se/dynamic_sql.html#objectnames

+2
source

All Articles