to winner2050(winner):
在你的SQL Statement的前面加上 SET PARSEONLY ON ,然后像平常一样在程序里执行(放心,它不会真的执行的,只是检查语法)这条SQL,如果有错误就是语法错误,不就可以了?你查一下SQL Help Online就知道了,加上这个后你的SQL只会做验证语法处理(当然也不会去检查你的表啊,字段之类的),不会执行的。你可以在SQL Query Analyzer中试一下如:写一个错误的SQL(table1不存在):select * from table1,你执行会报错,但是你加上Set Parseonly on在前面就不会了。其实还有用SQLMDO object的里面的接口来实现的,但是我忘了,现在也找不到了。我想如果能找到后面的方法,我觉得更好,那样就不必须利用System.Data之类的了,只要用SQLMDO object就可以做到,更方便简洁代码更容易让人理解。大伙去找后面的吧,一定有的。找到了就在这里告诉俺一声!谢了!
I'm working on a server-side application that dynamically builds complex SQL queries, which take painfully long to execute for testing.
Thus, I jumped to the idea in the post script of Chapter 10.2 to use PrepareStatement to just verify correctness of the SQL commands.
I found out that this approach is limited to the database environment, i.e. if the JDBC driver supports precompilation of prepared statements on the server-side before executing the commmand.
It seems that pure-Java JDBC-drivers don't support this feature. I tried PostgreSQL-JDBC and the pure-Java DB2 driver. They don't support it.
In contrast, the so-called "app-driver" of DB2-JDBC (COM.ibm.db2.jdbc.app.DB2Driver) supports it. But only, if you set DEFERREDPREPARE to 0 in the local db2cli.ini.
Since the RDMS of our application is DB2, I can use this technique. Which is a big improvement as to testing the correctness of SQL.