user25407 user25407 - 1 year ago 144
MySQL Question

execute stored procedure in google apps script

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);

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

Answer Source

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 ( {
            System.out.println(String.format("%s - %s",
                    rs.getString("first_name") + " "
                    + rs.getString("last_name"),
    } catch (SQLException ex) {

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download