通过java创建sql server表函数
我使用了一个简单的 sql 语句来测试是否可以通过 java 创建表函数。
但是,创建函数需要很长时间才能完成,并导致其他数据库用户超时。
有什么想法吗?
我正在使用 sqlserver 2008 express
try {
Statement statement = conn.createStatement();
CallableStatement cs ;
String retValue ;
cs = conn.prepareCall("{? = call dbo.isTableFunctionExists(?)}");
cs.registerOutParameter(1, Types.INTEGER);
lcString = "PAY_UDTF_"+this.textField1.getValue().toString() ;
cs.setString(2, lcString);
cs.execute();
retValue = cs.getString(1);
if (retValue.equals("1")) {
System.out.println("EXISTS");
lcSql = " ALTER ";
}else{
System.out.println("NOT FOUND");
lcSql = " CREATE ";
}
lcSql = lcSql + " FUNCTION [dbo].[" ;
lcSql = lcSql + lcString +"] (@pDate date)";
lcSql = lcSql + " RETURNS TABLE AS RETURN (";
lcSql = lcSql + " SELECT * FROM dbo.HR_EMPLOYMENT hre ";
lcSql = lcSql + " Where @pDate between hre.effective_start_date and hre.effective_end_date) ";
//statement.execute(lcSql);
statement.executeUpdate(lcSql);
statement.close();
System.out.println("COMPLETED");
} catch (Exception e) {
System.out.println("EXCEPTION"+e);
}
return null;
非常感谢任何帮助。
谢谢,
埃尔默
I used a simple sql statement for testing purposes if it is possible to create a table function thru java.
However, it takes a very long time for the creation of function to finish and causes other db users to time-out.
Any thoughts?
I'm using sqlserver 2008 express
try {
Statement statement = conn.createStatement();
CallableStatement cs ;
String retValue ;
cs = conn.prepareCall("{? = call dbo.isTableFunctionExists(?)}");
cs.registerOutParameter(1, Types.INTEGER);
lcString = "PAY_UDTF_"+this.textField1.getValue().toString() ;
cs.setString(2, lcString);
cs.execute();
retValue = cs.getString(1);
if (retValue.equals("1")) {
System.out.println("EXISTS");
lcSql = " ALTER ";
}else{
System.out.println("NOT FOUND");
lcSql = " CREATE ";
}
lcSql = lcSql + " FUNCTION [dbo].[" ;
lcSql = lcSql + lcString +"] (@pDate date)";
lcSql = lcSql + " RETURNS TABLE AS RETURN (";
lcSql = lcSql + " SELECT * FROM dbo.HR_EMPLOYMENT hre ";
lcSql = lcSql + " Where @pDate between hre.effective_start_date and hre.effective_end_date) ";
//statement.execute(lcSql);
statement.executeUpdate(lcSql);
statement.close();
System.out.println("COMPLETED");
} catch (Exception e) {
System.out.println("EXCEPTION"+e);
}
return null;
Any help is highly appreciated.
Thanks,
Elmer
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有一行设置 conn.setAutoCommit(false) ;
在创建函数结束时,我只需发出 conn.commit();
无论如何,非常感谢......
埃尔默
There's a line that sets the conn.setAutoCommit(false) ;
At the end of the creation of function, I simply issue conn.commit();
Big thanks anyway....
Elmer