譬如: 我Alert一个Column的Nullable后, 需要运行如下语句:
CALL SYSPROC.ALTOBJ ( ‘APPLY_CONTINUE_ON_ERROR’, ‘CREATE TABLE ADMINISTRATOR.TB6 (
ID BIGINT NOT NULL WITH DEFAULT 26 ,
NAME VARCHAR (128) NOT NULL WITH DEFAULT ‘TOM’) IN USERSPACE1 COMPRESS YES ‘, -1, ? );
注意看, NAME中名字都将默认叫做TOM, 但不论通过JDBC还是DB2Control Center运行, 都会出现错误:
SQL0104N An unexpected token "TOM" was found following "NULL WITH DEFAULT ‘"….
User response:
Examine and correct the statement in the area of the specified token.
sqlcode: –104
sqlstate: 42601
这不是让我费解的问题, 平日里咱最擅长的就是制造一大堆问题. 但DB2CC很贱, 生成出来的SQL语句就是这样, 但它死活就是有错误.
解决方法:
对普通的SQL来说, 无需转义, 可直接运行; 但作为存储过程的参数, 所以必须先转义(不论运行在DB2CC还是通过JDBC)
否则这句代码就紊乱了, 引号之间就被TOM搞乱套了.
正解:(注意看引号 ‘TOM’ => ”TOM”)
CALL SYSPROC.ALTOBJ ( ‘APPLY_CONTINUE_ON_ERROR’, ‘CREATE TABLE ADMINISTRATOR.TB6 (
ID BIGINT NOT NULL WITH DEFAULT 26 ,
NAME VARCHAR (128) NOT NULL WITH DEFAULT ”TOM”) IN USERSPACE1 COMPRESS YES ‘, -1, ? );
SQL语句: "SELECT service_level, fixpack_num FROM TABLE (sysproc.env_get_inst_info()) as INSTANCEINFO";
ALTER TABLE TABLE2 DROP PRIMARY KEY
ALTER TABLE TABLE2 ADD ID2 INTEGER NOT NULL DEFAULT 1;
ALTER TABLE TABLE2 ADD PRIMARY KEY(ID2);
See: http://bytes.com/topic/db2/answers/670160-how-change-primary-key-already-existing-table
直接使用metadata.getTables似乎不能获取DB2中指定Database下的所有Table, 但可使用如下语句获取:
"SELECT CREATOR,NAME FROM SYSIBM.SYSTABLES WHERE CREATOR NOT LIKE ‘SYS%’";
参考链接:
View Tables of DB2 Database using JDBC http://bytes.com/topic/db2/answers/704725-view-tables-db2-database-using-jdbc
支持数据库:
SQL Server, MySQL, PostgreSQL, Oracle, DB2, ANSI SQL, ODBC
地址:http://www.petefreitag.com/tools/sql_reserved_words_checker/
演示:
// Proudly powered by Apache, PHP, MySQL, WordPress, Bootstrap, etc,.