>Could you please provide me the sample of code how to declare temporary table in XSJS and call store procedure with input and output of tablevariables.
Actually there isn't really much JavaScript to this. Its all done via SQL Statements. Here is a sample. I didn't write it; but an SAP colleague posted it to our internal forums. In this case he is also creating the procedure directly in the catalog. This isn't something I'd recommend in a real application, but is nice for a self contained example.
- $.response.contentType = "text/plain";
- var result = "";
- var conn=$.db.getConnection();
- function onesql(sql) {
- try {
- var ps = conn.prepareStatement(sql);
- var execrc = ps.execute();
- ps.close();
- }catch(e){
- result += sql + ":\n" + e.toString() + "\n--------\n\n";
- }
- }
- // cleanup
- onesql("DROP TYPE tt_test");
- onesql("DROP PROCEDURE example");
- // create type and procedure
- onesql("CREATE TYPE tt_test AS TABLE (foo varchar(100))");
- onesql("CREATE PROCEDURE example(\n" +
- " IN in_table_1 tt_test,\n" +
- " IN in_table_2 tt_test,\n" +
- " IN max_rows int,\n" +
- " OUT out_table_1 tt_test,\n" +
- " OUT out_table_2 tt_test,\n" +
- " OUT total_input_rows int\n" +
- " ) LANGUAGE SQLSCRIPT READS SQL DATA AS\n" +
- "BEGIN\n" +
- " out_table_1 = SELECT TOP :max_rows * FROM :in_table_1;\n" +
- " out_table_2 = SELECT TOP :max_rows * FROM :in_table_2;\n" +
- " SELECT c1+c2 INTO total_input_rows FROM\n" +
- " (SELECT COUNT(*) AS c1 FROM :in_table_1),\n" +
- " (SELECT COUNT(*) AS c2 FROM :in_table_2);\n" +
- "END;");
- // create some local input tables for a test call
- onesql("CREATE LOCAL TEMPORARY TABLE #local_test_table_1(foo varchar(100))");
- onesql("INSERT INTO #local_test_table_1 VALUES('bla')");
- onesql("INSERT INTO #local_test_table_1 VALUES('blubb')");
- onesql("CREATE LOCAL TEMPORARY TABLE #local_test_table_2(foo varchar(100))");
- onesql("INSERT INTO #local_test_table_2 VALUES('bla')");
- // call the procedure with the local data
- try{
- var pc = conn.prepareCall("CALL example(#local_test_table_1, #local_test_table_2, ?, ?, ?, ?)");
- var pmd = pc.getParameterMetaData();
- result += "Got " + pmd.getParameterCount() + " unbound parameters\n";
- result += "1: " + pmd.getParameterName(1) + "-" + pmd.getParameterTypeName(1) + "\n";
- result += "2: " + pmd.getParameterName(2) + "-" + pmd.getParameterTypeName(2) + "\n";
- result += "3: " + pmd.getParameterName(3) + "-" + pmd.getParameterTypeName(3) + "\n";
- result += "4: " + pmd.getParameterName(4) + "-" + pmd.getParameterTypeName(4) + "\n";
- pc.setBigInt(1, 2);
- result += "\n\n---------\n\nResults:\n\n";
- if(pc.execute()) {
- result += "total_input_rows: " + pc.getBigInt(4) + "\n";
- do {
- var rs = pc.getResultSet();
- result += "=========\n";
- result += "ResultSet has: " + rs.getMetaData().getColumnCount() + " column(s)\n";
- //result += rs.getMetaData().getColumnTypeName(1);
- while(rs.next()) {
- result += rs.getString(1) + "\n";
- }
- result += "=========\n\n";
- } while (pc.getMoreResults()); // get next resultset from stored procedure
- } else {
- result += "Failed to execute procedure";
- }
- } catch (e) {
- result += e.toString();
- }
- $.response.setBody(result);