Calling SQL stored procedure dynamically

Hallo,
could anybody tell me how to call HANA SQL stored procedure dynamically?
The coding looks like this:
v_ProcedureName := '"' || KpiNamespace || '::' || KpiName || '"';
v_SQL := 'CALL ' || :v_ProcedureName || '( lt_outKPI )';
EXEC :v_SQL;
For this call I get the error message:
"Transaction rolled back by an internal error: wrong number or types of parameters in call: Physical table is not allowed in OUT table variable position: LT_OUTKPI: line 1 col 55 (at pos 54)"
The procedure to be called dynamically has a table output parameter.
Thanks in advance,
Best Regards,
Alexey Romanov.
Hi Alex,
Can you share your requirement on exactly what is the need to call the procedure dynamically?
For your question, have a look on the below:
1) Have a procedure named EMPLOYEE_DETAILS
http://scn.sap.com/servlet/JiveServlet/downloadImage/2-14895702-415799/236-170/pastedImage_0.png
2) Created one more procedure named Dynamically as shown below:
CREATE PROCEDURE Dynamically ()
AS
BEGIN
DECLARE QUERY VARCHAR(1000);
QUERY := 'call  EMPLOYEE_DETIALS(?)';
EXECUTE IMMEDIATE (:QUERY);
END;
3) Output:
http://scn.sap.com/servlet/JiveServlet/downloadImage/2-14895702-415800/210-189/pastedImage_3.png
Regards,
Krishna Tangudu
Hi Krishna,
Thanks for your reply!
Unfortunately, I can neither go to ‘full discussion’, nor open the pictures, you have attached, nor see you reply.
That’s why I just add my reply to the problem description.
The procedure name is composed out of 2 parts on-the-fly, that is why I have to call it dynamically:
create local temporary table "#KPIEvals"(           
          "KpiNamespace" NVARCHAR(256) CS_STRING,           
          "KpiName" NVARCHAR(32) CS_STRING,               
          "KpiText" NVARCHAR(256) CS_STRING,           
          "Responsible" NVARCHAR(256) CS_STRING );
FOR cur_row as c_cursor DO        
     v_SQL := '';     
     v_ProcedureName :=  '"' || cur_row."KpiNamespace" || '::' || cur_row."KpiName" || '"';     
     v_SQL := 'CALL ' || :v_ProcedureName || '( lt_outKPI )';     
     EXEC :v_SQL;                
     v_SQL := 'INSERT INTO "#KPIEvals" SELECT * FROM :lt_out_kpi';     
     EXEC :v_SQL;                
END FOR;
I guess there might be 2 reasons for the problem:
1.     All the procedures are called dynamically in a loop.
2.     All the procedures have an output parameter, which is a table.
Hope that helps,
Best Regards,
Alex.

Hi Alexey, I think what you may need to do in this case is to do the insert into your temp table from within the dynamically called procedure, therefore no need for output param in your procedure call. You can then select from the temp table in your main procedure. Hope that makes sense.
Peter

Similar Messages

  • Calling SQL Stored Procedure using Oracle Gateway

    Hi
    Do you know how i can call a stored procedure with parameters from Oracle using oracle gateway?

    Don't know which gateway you are using. Only the transparent gateway for SQL Server and Sybase that have support for stored procedures.
    Take a look at case 7 which shows how to do this.
    Rem case7.sql
    Rem
    Rem Copyright (c) Oracle Corporation 2000. All Rights Reserved.
    Rem
    Rem NAME
    Rem case7.sql
    Rem
    Rem DESCRIPTION
    Rem SQL script which executes the demo case7 for the
    Rem Transparent Gateways
    Rem
    Rem NOTES
    Rem The database link GTWLINK should be created before you can
    Rem run this demo file
    Rem
    Rem MODIFIED (MM/DD/YY)
    Rem kpeyetti 11/09/00 - Created
    Rem
    SET ECHO ON
    DROP TABLE LOCAL_GTW_DEPT;
    CREATE TABLE LOCAL_GTW_DEPT (DEPTNO INTEGER, DEPTNAME VARCHAR2(14));
    SELECT * FROM LOCAL_GTW_DEPT;
    DECLARE
    DNAME VARCHAR2(14);
    BEGIN
    "GetDept"@GTWLINK(10,DNAME);
    INSERT INTO LOCAL_GTW_DEPT VALUES (10, DNAME);
    END;
    SELECT * FROM LOCAL_GTW_DEPT;

  • Calling SQL stored procedure.

    Seems straightforward but I just can get the syntax.
    Here's the procedure defined in SQL
    CREATE Procedure usp_InsTest
    @title     varchar(150),
    @length     int,
    @catid     int,
    @date     datetime,
    @filename     varchar(50),
    @typeid          int,
    @desc          varchar(2000),
    @statusid     int,
    @assign          char(1),
    @languageid     int = null
    as
    Insert into test(TestTitle,TestLength,TestCatID,TestDate,TestFileName,
              TestTypeID,TestDesc,TestStatusID,TestIsAssigned,
              TestLanguageID)
    values (@title,@length,@catid,@date,@filename,@typeid,@desc,
         @statusid,@assign,@languageid)
    GO
    I have tried a number of ways. What is the correct sytax for calling a stored procedure with variables?

    Here's what I've come up with...
    Connection dbConn;
                   Driver d = (Driver)Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
                   String URL = "jdbc:odbc:" + "cms";
                   dbConn = DriverManager.getConnection(URL, "sa", "");
                   CallableStatement s;
                   String x = "{call usp_InsTest(@title,@length,@catid,@date,@filename,@typeid,@desc,@statusid,@assign,@languageid)}";
                   Calendar c = Calendar.getInstance();
                   s = dbConn.prepareCall(x);
                   s.setString(1,TestTitle);
                   s.setInt(2,Integer.parseInt(TestLength));
                   s.setInt(3,6);
                   s.setDate(4,new java.sql.Date(System.currentTimeMillis()));
                   s.setString(5,TestFileName);
                   s.setInt(6,Integer.parseInt(TestTypeID));
                   s.setString(7,"Test Description");
                   s.setInt(8,Integer.parseInt(TestStatusID));
                   s.setInt(9,Integer.parseInt(TestIsAssigned));
                   s.setInt(10,1);
                   ResultSet r = s.executeQuery();
    I get
    java.lang.NullPointerException
         at sun.jdbc.odbc.JdbcOdbcPreparedStatement.clearParameter(Unknown Source)
         at sun.jdbc.odbc.JdbcOdbcPreparedStatement.setChar(Unknown Source)
         at sun.jdbc.odbc.JdbcOdbcPreparedStatement.setString(Unknown Source)
         at CMSBatch.addToDatabase(CMSBatch.java:242)
         at CMSBatch.actionPerformed(CMSBatch.java:312)
         at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
         at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(Unknown Source)
         at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
         at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
         at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
         at java.awt.Component.processMouseEvent(Unknown Source)
         at java.awt.Component.processEvent(Unknown Source)
         at java.awt.Container.processEvent(Unknown Source)
         at java.awt.Component.dispatchEventImpl(Unknown Source)
         at java.awt.Container.dispatchEventImpl(Unknown Source)
         at java.awt.Component.dispatchEvent(Unknown Source)
         at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
         at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
         at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    Line 242 is the first setString.
    Is my prepareCall statement correct?

  • Calling SQL Stored procedure from PLSQL function

    Hi,
    I have created a simple function to return a value that is pulled back from SQL server.
    FUNCTION
    FUNCTION CALC_PASS_FAIL
    i_lidded_sn IN TMP_CHIP_ATTR.LIDDED_SN%TYPE
    RETURN NUMBER
    IS
    tmp_result NUMBER;
    BEGIN
    [email protected](i_lidded_sn,'OadmBarActive',tmp_result);
    RETURN tmp_result;
    END CALC_PASS_FAIL;
    OUTPUT
    --When executed as a procedure, i get the required output:
    DECLARE
    x INT;
    BEGIN
    x:=EQDAS_MISC.CALC_PASS_FAIL('OADM004548');
    dbms_output.put_line(x);
    END;
    0 <------ expected output
    PL/SQL procedure successfully completed.
    When executed as a simple function call, i don't get any output:
    SELECT CALC_PASS_FAIL ('OADM004505') FROM dual;
    Any input will be greately appretiated.
    Thanks,
    Shashi
    PS: I am using the DB Link setup usign DG4MSQL

    just wondering it works as procedure as the function misses double quotes:
    dbo.GetChipPareto
    should be
    "dbo"."GetChipPareto"
    Message was edited by:
    kgronau
    Tested a small example using a function like:
    create or replace function test(
    v_ut1 in varchar2)
    RETURN varchar2
    IS
    v_ut2 varchar2(255);
    BEGIN
    "dbo".OTG_TEST3@dg4msql( v_ut1, v_ut2);
    RETURN v_ut2;
    END test;
    The function calls a SQL Server procedure OTG_TEST3 just returning the passed string and it guarantees data integrity.
    Result using a stored procedure:
    SQL> DECLARE
    2 ret integer;
    3 v_ut1 varchar2(255);
    4 v_ut2 varchar2(255);
    5 BEGIN
    6 v_ut1 :='Hello World';
    7 v_ut2 :='';
    8 "dbo".OTG_TEST3@dg4msql( v_ut1, v_ut2);
    9 dbms_output.put_line('Input value: ' ||v_ut1||' - Output value: '
    ||v_ut2);
    10 END;
    11
    12 /
    Input value: Hello World - Output value: Hello World
    And calling now the function:
    select test('Hello') from dual;
    returns Hello

  • How to run oracle stored procedure dynamically

    How can I call a stored procedure dynamically. My requirement is to call a stored procedure based on user selection on a parameter screen. I do not want to hardcode proocedure name, but rather call dynamically.
    If I use the same methodology as of running dynamic sql's Iam getting an error.
    "invalid SQL statement after call to procedure"
    v_cursorid INTEGER;
    v_cnt NUMBER;
    v_sqlstring VARCHAR2(2000);
    v_sqlstring := 'execute insert_job_status('''||TO_CHAR(SYSDATE,
    'dd-mon-yyyy HH:mi:ss')||''');';
    v_cursorid := dbms_sql.open_cursor;
    dbms_sql.parse(v_cursorid, v_sqlstring, dbms_sql.v7);
    --cnt        := dbms_sql.execute(v_cursorid);
    dbms_sql.close_cursor(v_cursorid);

    EXECUTE is a SQL*Plus command. try
    v_sqlstring := 'begin  insert_job_status('''||TO_CHAR(SYSDATE,
    'dd-mon-yyyy HH:mi:ss')||'''); end;';Cheers, APC

  • Call stored procedure dynamically?

    I wrote a bunch of programs in Perl to output different reports. All these programs are very similar: calling a stored procedure in SQL Server, dump the ResultSet into the file in disk ...
    My boss asked me to write a "Generic" program to combine these programs together so that we don't need write endless similar programs any more.
    I decide to write this program in Java. The new program is supposed to take a stored procedure call as an input parameter (reading from a configuration file, maybe?). It will be great if the program can dynamically determine the field names in the ResultSet from the stored procedure call. In another word, there shouldn't be any hard coded field names in the Java Code. The more generic of the program, the better.
    Is this doable? Does anyone have any such experience before? Any idea or recommendation is highly appreciated.
    Thanks a lot,
    - LX

    Here is some that I wrote:
    //Here is a snippet out of my main program:
                   md = rs.getMetaData();
                   noOfColumns = md.getColumnCount();     
                   //Generate the sql for our prepared statement insert
                   sql = buildInsertSQL("DWXP111", noOfColumns);
                   lowLevel.info("SQL:" + sql);
                   //Create the prepared statment
                   ps = legacyCon.prepareStatement(sql);
                   //Set the prepared statement input parameters
                   while(rs.next())
                        for(int x=1; x<noOfColumns+1; x++)
                             setParameters(x, md.getColumnType(x));     
                        //Execute the prepared statement
                        checkForSuccess(ps.executeUpdate());                    
    //Here is the method it calls:
         private void setParameters(int columnNo, int dataType)
              lowLevel.info("Setting parameter number: " + columnNo);
              lowLevel.info("Data type:" + dataType);
              try
                   switch(dataType)
                        case Types.VARCHAR:
                             ps.setString(columnNo, rs.getString(columnNo)==null ? "" : rs.getString(columnNo).toUpperCase());
                             lowLevel.info("String set:" + rs.getString(columnNo));
                             break;
                        case Types.DECIMAL:
                             ps.setString(columnNo,  rs.getBigDecimal(columnNo)==null ? "0" : rs.getBigDecimal(columnNo).toString());
                             lowLevel.info("Decimal set");
                             break;
                        case Types.CHAR:
                             ps.setString(columnNo,  rs.getBigDecimal(columnNo)==null ? "0" : rs.getBigDecimal(columnNo).toString());
                             lowLevel.info("Char set");
                             break;
              catch (SQLException e)
                   lowLevel.warn("Error while setting parameters for " + dataType + " info");
                   e.printStackTrace();
         }

  • Calling a stored procedure on SQL 2008 server

    This might be easy one, but I tried different things just cannot get it to work.
    I am trying to call a stored procedure in SQL 2008 R2 server using Type 4 driver (sqljdbc4.jar) with JR.
    The stored procedure basically has one input and two outputs
    ALTER PROCEDURE [dbo].[usp_Example_Master]
    @XMLResponse as varchar(max),
    @ERROR_NUMBER as int OUTPUT,
    @ERROR_MESSAGE as varchar(300) OUTPUT
    as
    My program.
    Connection connection =null
    CallableStatement cs =null;
    connection = DriverManager.getConnection(url, username, password);
    cs = connection.prepareCall("{call dbo.usp_Example_Master(?,?,?)}");
    cs.setString(1,requestStr);
    cs.registerOutParameter(2, Types.INTEGER);
    cs.registerOutParameter(3, Types.VARCHAR);
    cs.execute();
    ============
    Error: The output parameter 1 was not registered for output.
    But if I added cs.registerOutParameter(1, Types.VARCHAR);
    Error: The formal parameter "@XMLResponse" was not declared as an OUTPUT parameter, but the actual parameter passed in requested output
    ==========
    Appreciate any help !

    Ok. I have an alternative to get it to work now.
    Instead of using index number, I am using the parameter name now. It works as expected and more intuitive any way.
    Ex.
    cs.setString("XMLResponse",req);
    cs.registerOutParameter("ERROR_NUMER",Type.INTEGER);
    Edited by: 903857 on Dec 21, 2011 2:45 PM

  • T-sql 2008 r2 place results from calling a stored procedure with parameters into a temp table

    I would like to know if the following sql can be used to obtain specific columns from calling a stored procedure with parameters:
    /* Create TempTable */
    CREATE TABLE #tempTable
    (MyDate SMALLDATETIME,
    IntValue INT)
    GO
    /* Run SP and Insert Value in TempTable */
    INSERT INTO #tempTable
    (MyDate,
    IntValue)
    EXEC TestSP @parm1, @parm2
    If the above does not work or there is a better way to accomplish this goal, please let me know how to change the sql?

    declare @result varchar(100), @dSQL nvarchar(MAX)
    set @dSQL = 'exec @res = TestSP '''+@parm1+''','' '''+@parm2+' '' '
    print @dSQL
      EXECUTE sp_executesql @dSQL, N'@res varchar(100) OUTPUT', @res = @result OUTPUT
    select @result
    A complicated way of saying
    EXEC @ret = TestSP @parm1, @parm2
    SELECT @ret
    And not only compliacated, it introduces a window for SQL injection.
    Erland Sommarskog, SQL Server MVP, [email protected]

  • How to write a PL/SQL stored procedure in Oracle to call Webservice

    Can any one pelase send me a code on how to write a PL/SQL stored procedure in Oracle database to call the Webservice ?
    Thanks,
    Rajesh

    Were you able to solve this problem

  • How to call a sql stored procedure in java...... HELP

    Hi I am making an application for taking backup in sql automatically so i have created a dts package which is called by a stored procedure. Now the problem is that how to call that stored procedure in a Java program so that after running my java program i get my database backup.
    Please please solve my problem.
    thanks in advance.
    If possible please send the code.
    Message was edited by:
    Andy_Davis
    Message was edited by:
    Andy_Davis

    Hi... I am trying to create a dts package which is called by a stored procedure... How can i do this? IF possible can you please send me the code as well..
    Thanks a ton...
    Susan_Davis

  • How to call pl/sql stored procedure in JDBC query dialogbox

    Hi,
    how to call pl/sql stored procedure in JDBC query dialogbox(reports 9i) .
    Cheers,
    Raghu

    please refer : Re: problem If you have more doubts, please ask in that question.

  • How to call PL/SQL stored procedure using ODBC?

    Could anyone tell me how can I call PL/SQL stored procedure using
    ODBC? Are there any sample codes?
    Thanx!
    null

    You are correct on all counts, they all should work.
    Oracle Product Development Team wrote:
    : Hi,
    : I don't know the exact syntax in ODBC, but reasoning by analogy
    : with other API's, I'd bet one of the following works
    : (for a call to: procedure my_proc(n1 number, n2 number);):
    : "{ my_proc(1,2); }"
    : "{ call my_proc(1,2); }"
    : "{ begin my_proc(1,2); end }"
    : "begin my_proc(1,2); end;"
    : "begin my_proc(1,2); end"
    : Hope this helps. - Pierre
    : jiangbuf (guest) wrote:
    : : Could anyone tell me how can I call PL/SQL stored procedure
    : using
    : : ODBC? Are there any sample codes?
    : : Thanx!
    : Oracle Technology Network
    : http://technet.oracle.com
    null

  • How to call PL-SQL/stored procedure in Creator

    Anybody can tell how to call PL-SQL/Stored procedures inside creator...

    Hi!!!
    You can see this topic http://forum.sun.com/jive/thread.jspa?threadID=106046
    There is how to call oracle stored procedures. Also I put a lot of links in these topic doing reference stored procedures. I have one that it tells specially how to call oracle stored procedures from java, is in spanish but you can understand the code.;-)
    http://yoprogramador.vampisol.com/index.php?title=pl_sql_oracle_desde_java&more=1&c=1&tb=1&pb=1
    Byeee

  • Calling stored procedure dynamically

    Hello,
    I have a feeling this is not possible but was wondering if it is possible to have a call within a package to a stored procedure that is outside the said package? And can a stored procedure outside a package call another stored procedure that is also outside a package?
    Reason is, i have a couple of packages that both contain a procedure that is exactly the same so for efficiency can i have the stored procedure that is used by both packages stored outside the packages where it can be called by them both?
    Thanks in advance

    Ah!!! So if i had for example, a stored procedure called total_Customers in Package1 and required to use this >in Package2, i could then have a call within Package2 that said;
    Package1.total_CustomersYes, if the procedure total_Customers in Package1 has been declared in the package specification
    I recommend you to study the PL/SQL User's Guide and Reference
    What Is a PL/SQL Package?
    http://download-uk.oracle.com/docs/cd/B10501_01/appdev.920/a96624/09_packs.htm#362
    Message was edited by:
    Jens Petersen

  • Error calling PL/SQL stored procedure

    Hi,
    I'm pretty new to JDeveloper having used NetBeans for the last few years.
    I'm getting an error when trying to call a PL/SQL stored procedure. When I click on any of the oracle errors I get an "unable to find source file" message. The errors occurs in call.execute(). The error message and Java code are listed below. Any help is much appreciated.
    Cheers,
    Stevie
    Errors:
    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 33
         at java.lang.String.charAt(String.java:558)
         at oracle.jdbc.driver.OracleSql.handleODBC(OracleSql.java:877)
         at oracle.jdbc.driver.OracleSql.parse(OracleSql.java:811)
         at oracle.jdbc.driver.OracleSql.getSql(OracleSql.java:284)
         at oracle.jdbc.driver.OracleSql.getSqlBytes(OracleSql.java:538)
         at oracle.jdbc.driver.T4CCallableStatement.doOall8(T4CCallableStatement.java:169)
         at oracle.jdbc.driver.T4CCallableStatement.execute_for_rows(T4CCallableStatement.java:873)
         at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1161)
         at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3001)
         at oracle.jdbc.driver.OraclePreparedStatement.execute(OraclePreparedStatement.java:3093)
         at oracle.jdbc.driver.OracleCallableStatement.execute(OracleCallableStatement.java:4286)
         at oracle.CallPLSQL.callLMO(CallPLSQL.java:118)
         at oracle.TestHarness.main(TestHarness.java:18)
    Jave Code:
    package oracle;
    import java.sql.CallableStatement;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.sql.Types;
    public class CallPLSQL {
    private Connection conn;
    public CallPLSQL() {
    *Constructor.
    try {
    //Register Oracle Database Drive
    DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
    //set connection
    String connectionString="jdbc:oracle:thin:TSD/TSD@DEV:1526:DEVELOP";
    this.setConn(DriverManager.getConnection(connectionString,"TSD", "TSD"));
    } catch (SQLException ex) {
    public int callproc () throws SQLException {
    CallableStatement call;
    String callString = "{call proc" +
    call = conn.prepareCall(callString);
    call.setInt(1, 120289);
    call.setInt(2, 2008);
    call.registerOutParameter(3, Types.INTEGER);
    call.registerOutParameter(4, Types.INTEGER);
    call.registerOutParameter(5, Types.INTEGER);
    call.registerOutParameter(6, Types.DATE);
    call.registerOutParameter(7, Types.VARCHAR);
    call.registerOutParameter(8, Types.INTEGER);
    call.registerOutParameter(9, Types.INTEGER);
    call.execute();
    int i = call.getInt("app_id");
    return i;
    public void setConn(Connection conn) {
    this.conn = conn;
    public Connection getConn() {
    return conn;
    }

    I've fixed it now. I'd missed a } in prepareCall. What a doughball.

Maybe you are looking for

  • How do I burn a .mov file to DVD?

    I need to burn an MPEG-2 video file saved as .mov to a DVD that will play in a regular DVD player. I have tried opening iDVD and the selecting 'Create a New Project'. When I have selected a location for the project and the main window opens, I go to

  • How to delete the queries in  BW Production which are no longer existing.

    Hi, How to  delete the queries in BW production which are no longer existing in DEV. 1. I tried Using RSZDELETE in Production it is not getting deleted and the below message given. Query object 4A7V83T3RB4ABIOKSDJT2HWDL is blocked. Deletion has been

  • The data back up to info cube

    hi.. sir i have created PA, also created a Time series for the PA.  and then created PB, DV. Now i want to add a KF to  the PA. How?? if i deactivate the planning area i will loose the time series data. then how i can back up the time series data to

  • Missing business area in FG line item

    Hi All, We are starting a new company code that implements business areas. We've linked the business areas to our plants and division and also configured that the sales area picks up the business area from our plants and division. However, when we po

  • Ichat-disconnected

    HI, my ichat account was working fine until today. I clicked on the application and it took 5 minutes to load. Then, I got a message that "The connection could not be completed because it timed out. Try again." I tried to restart my computer multiple