• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Passing Table Names in PreparedStatement

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I was trying to pass a table name in the query dynamically like

query = "select * from ?";
PreparedStatement pstmt = connection.prepareStatement(query);
pstmt.setString(1,"table1");

It Throws an Exception
"Exception in thread "main" com.sybase.jdbc3.jdbc.SybSQLException: ASA Error -141: Table '@p0' not found"

Is there a way to pass the table name dynamically?
Thanks in Advance
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Senthil,
We cannot pass tables as bind variable using prepared statement..
Instead form the query string dynamically.
eg: query="select * from "+var1;
 
Ranch Hand
Posts: 341
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well we can't pass the tablename directly to prepared statements.

But, if you want to use stored procedures then you can pass table name dynamically using Dynamic SQL.

For further reading of Dynamic SQL refer this article on Dynamic SQL.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic