sql server - How can I pass a table name as a variable in SQL - Python 3.4 -
i trying write sql statement in python passes table name variable. however, following error: must declare table variable "@p1".
pypyodbc.programming error: ('42000', '[42000]' [miscrosoft] [sql server native client 10.0] [sql server] must declare table variable "@p1"
the code yielding error is:
query = cursor.execute('''select * ?''', (table_variable,))
i have other code pass variables sql statement using same syntax works fine (code below works intended).
query = cursor.execute('''select column_name information_schema.columns table_name = ?''', (table_variable,))
the error seems occur when using variable pass table name.
any resolving error appreciated.
what you're trying impossible. can pass values queries parameters -
select * @table
is banned but
select * tablename column=@value
is legal.
now, why it's banned. logical point of view database layer can't cache query plan you're trying @ - parameter , utterly change goes , returns - , can't guarantee in advance can or can't do. it's trying load abstract source file @ runtime , execute - messy, unpredictable, unreliable , potential security hole.
from reliability point of view, please don't do
select * table
either. makes code less readable because can't see what's coming where, less reliable because change without warning , break application.
i know can seem long way round @ first, - writing individual select statements specify fields want bring better way it. it'll make application run faster :-)
Comments
Post a Comment