Search code examples
javasql-server-2008-express

create sql server table function thru java



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


Solution

  • 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