Quantcast
Channel: SCN: Message List - SAP HANA Developer Center
Viewing all articles
Browse latest Browse all 9165

Re: How to call HANA store procedure with table variables in xsjs?

$
0
0

>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.

 

  1. $.response.contentType = "text/plain"
  2. var result = ""
  3.  
  4. var conn=$.db.getConnection(); 
  5.  
  6. function onesql(sql) { 
  7.     try
  8.         var ps = conn.prepareStatement(sql); 
  9.         var execrc = ps.execute(); 
  10.         ps.close(); 
  11.     }catch(e){ 
  12.         result += sql + ":\n" + e.toString() + "\n--------\n\n"
  13.     } 
  14.  
  15. // cleanup 
  16. onesql("DROP TYPE tt_test"); 
  17. onesql("DROP PROCEDURE example"); 
  18.  
  19. // create type and procedure 
  20. onesql("CREATE TYPE tt_test AS TABLE (foo varchar(100))"); 
  21. onesql("CREATE PROCEDURE example(\n"
  22.        "  IN  in_table_1 tt_test,\n"
  23.        "  IN  in_table_2 tt_test,\n"
  24.        "  IN  max_rows int,\n"
  25.        "  OUT out_table_1 tt_test,\n"
  26.        "  OUT out_table_2 tt_test,\n"
  27.        "  OUT total_input_rows int\n"
  28.        "  ) LANGUAGE SQLSCRIPT READS SQL DATA AS\n"
  29.        "BEGIN\n"
  30.        " out_table_1 = SELECT TOP :max_rows * FROM :in_table_1;\n"
  31.        " out_table_2 = SELECT TOP :max_rows * FROM :in_table_2;\n"
  32.        " SELECT c1+c2 INTO total_input_rows FROM\n" +  
  33.        "   (SELECT COUNT(*) AS c1 FROM :in_table_1),\n"
  34.        "   (SELECT COUNT(*) AS c2 FROM :in_table_2);\n"
  35.        "END;"); 
  36.  
  37.  
  38. // create some local input tables for a test call 
  39. onesql("CREATE LOCAL TEMPORARY TABLE #local_test_table_1(foo varchar(100))"); 
  40. onesql("INSERT INTO #local_test_table_1 VALUES('bla')"); 
  41. onesql("INSERT INTO #local_test_table_1 VALUES('blubb')"); 
  42.  
  43. onesql("CREATE LOCAL TEMPORARY TABLE #local_test_table_2(foo varchar(100))"); 
  44. onesql("INSERT INTO #local_test_table_2 VALUES('bla')"); 
  45.   
  46. // call the procedure with the local data 
  47. try
  48.     var pc = conn.prepareCall("CALL example(#local_test_table_1, #local_test_table_2, ?, ?, ?, ?)"); 
  49.     var pmd = pc.getParameterMetaData(); 
  50.     result += "Got " + pmd.getParameterCount()  + " unbound parameters\n"
  51.     result += "1: " + pmd.getParameterName(1) + "-" + pmd.getParameterTypeName(1) + "\n"
  52.     result += "2: " + pmd.getParameterName(2) + "-" + pmd.getParameterTypeName(2) + "\n"
  53.     result += "3: " + pmd.getParameterName(3) + "-" + pmd.getParameterTypeName(3) + "\n"
  54.     result += "4: " + pmd.getParameterName(4) + "-" + pmd.getParameterTypeName(4) + "\n"
  55.      
  56.     pc.setBigInt(1, 2); 
  57.     result += "\n\n---------\n\nResults:\n\n"
  58.      
  59.     if(pc.execute()) { 
  60.         result += "total_input_rows: " + pc.getBigInt(4) + "\n"
  61.          
  62.         do
  63.             var rs = pc.getResultSet(); 
  64.             result += "=========\n"
  65.             result += "ResultSet has: " + rs.getMetaData().getColumnCount() + " column(s)\n"
  66.             //result += rs.getMetaData().getColumnTypeName(1); 
  67.             while(rs.next()) { 
  68.                 result += rs.getString(1) + "\n"
  69.             } 
  70.             result += "=========\n\n"
  71.         } while (pc.getMoreResults());  // get next resultset from stored procedure 
  72.     } else
  73.         result += "Failed to execute procedure"
  74.     } 
  75. } catch (e) { 
  76.     result += e.toString(); 
  77.  
  78.  
  79. $.response.setBody(result); 



Viewing all articles
Browse latest Browse all 9165

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>