views:

48

answers:

1

I'm writing a subsystem that tables might be renamed from project to project.

Instead of asking the user of my subsystem to search & replace before using it, does this work?

<cfquery name="local.foo" datasource="#dsn#">
  SELECT col1, col2, col3
  FROM #tableName#
</cfquery>

Without <cfqueryparam>, will it become non-cacheable? or any other issues? (assume SQL-injection is not an issue)

I don't think I can use <cfqueryparam> for table name, right?

Thanks.

+1  A: 

That'll work, sure. CF simply converts all variables to their values and sends the string to the database driver.

Be very, very careful, though. As you've implied, this could set you up for some nasty SQL injection.

Al Everett
If you are using MySQL, you can escape the tableName to make it SQL safe by wrapping the string with backticks such as: `#tableName#`. Other databases will have their own ways of escaping table and column names.
Greg S