问题
INFORMIX-SQL or any other SQL-based DB:
Suppose I have an app where depending on the value of some columns, example:
company.code char(3) {abc}
company.branch char(2) {01}
Can I construct table name "abc01" for inclusion in SELECT * FROM abc01; ? In other words, a variable table name.. same question applies for column names.
回答1:
Only in a language which can manipulate character strings and handle Dynamic SQL. It has to create the statement on the fly.
You can only use placeholders in queries for values, not for the structural elements of the query such as the table name or column name.
回答2:
Only if you use dynamic sql. It's like you build sql code in your app, then use something like execute immediate.
sprintf(cdb_text1, "create table %s (field1 char(3));", usr_db_id);
EXEC SQL execute immediate :cdb_text;
If you use dynamic sql, it's bad because of sql injections.
来源:https://stackoverflow.com/questions/3059019/variable-table-or-column-names