Variable table or column names

元气小坏坏 提交于 2019-12-24 12:03:01

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!