execute stored procedure in google apps script

假如想象 提交于 2020-06-13 09:19:12

问题


I am using the below code in google apps script to insert employee record from a Google Sheet into a mysql database. The stored procedure [dbo].[insertemployee] accepts "@empname" parameter.

  var empname = 'test';
  var mysqldbconn = Jdbc.getConnection(url,username,password);
  var mysqlquery = mysqldbconn.createStatement(); 
  callstoredprocedure = "{call [dbo].[testemployee](?)}";
  mysqlquery = mysqldbconn.prepareCall(callstoredprocedure);
  
  mysqlquery.setString(1, empname);
  mysqlquery.executeUpdate();

This gives me this error "Must declare the scalar variable "@empname"".


回答1:


Perhaps reading this might help you: Calling MySQL Stored Procedures from JDBC

This is where you go wrong:

mysqlquery = mysqldbconn.prepareCall(vstrSQLStatement);

You don't use the declared 'callstoredprocedure'.

Read example below:

public static void getSkills(int candidateId) {
    // 
    String query = "{ call get_candidate_skill(?) }";
    ResultSet rs;

    try (Connection conn = MySQLJDBCUtil.getConnection();
            CallableStatement stmt = conn.prepareCall(query)) {

        stmt.setInt(1, candidateId);

        rs = stmt.executeQuery();
        while (rs.next()) {
            System.out.println(String.format("%s - %s",
                    rs.getString("first_name") + " "
                    + rs.getString("last_name"),
                    rs.getString("skill")));
        }
    } catch (SQLException ex) {
        System.out.println(ex.getMessage());
    }
}

Let me explain: The query is defined, similar to your call accepting 1 parameter returning no value. Then it is used in the: conn.prepareCall() , and then the parameter is set to the statement and then it is executed.

As I mentioned, you should do this:

mysqlquery = mysqldbconn.prepareCall(callstoredprocedure);

Please let me know, if this fixed your problem.



来源:https://stackoverflow.com/questions/40919246/execute-stored-procedure-in-google-apps-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!