I've created a store procedure in MySQL which expect some inputs and return some output. To call store procedure in MySQL i am running
CALL createCoupon(1236,321, @message);
SELECT @message AS message
and getting the output in message object.
now here comes the situation i need to call this SP in sequelize. I'm working on sailsjs project and using sequelize module for queering.
I've created the database connection in config/db_config , my connection string is:
var sequelize = new Sequelize(db.name, db.user, db.pass, {
host: db.host,
dialect: "mysql", // or 'sqlite', 'postgres', 'mariadb'
port: 3306, // or 5432 (for postgres)
maxConcurrentQueries: 100,
pool: {
maxConnections: 50,
maxIdleTime: 2000
},
queue: true
})
and i'm calling it in controller like:
var Sequelize = require('sequelize');
var sequelize = require('../../config/db_config').dbase;
function setCoupon(couponCode, userId, setCouponResponse) {
var createCouponSQL = "some raw query";
sequelize.query(createCouponSQL, null, {
raw: true
}).success(function(createCoupon) {
sails.log.info(createCoupon);
setCouponResponse(null, createCoupon);
}).error(function(err) {
sails.log.error(err);
setCouponResponse(err, null);
});
}
module.exports = {
'createCoupon': function(req, callback) {
setCoupon(req.param('coupon_code'), req.session.userSession, function(err, setCouponResponse){
})
}
}
now i need to call SP in sequelize so i simply try:
var createCouponSQL = "CALL createCoupon(1236,321, @message);";
createCouponSQL += "SELECT @message AS message";
sequelize.query(createCouponSQL, null, {
raw: true
}).success(function(createCoupon) {
sails.log.info(createCoupon);
setCouponResponse(null, createCoupon);
}).error(function(err) {
sails.log.error(err);
setCouponResponse(err, null);
});
but the sequlize trigger the error:
Executing (default): CALL createCoupon(1236,321, @message);SELECT @message AS m
essage
error: Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the m
anual that corresponds to your MySQL server version for the right syntax to use
near 'SELECT @message AS message' at line 1
at Query.Sequence._packetToError (C:\Users\asd\Desktop\CardCash P2\Website\
node_modules\mysql\lib\protocol\sequences\Sequence.js:30:14)
I've done some R&D but not able to find any thing proper to call a store procedure which is sending me back the response in @message and i need to execute another select statement to get the result.
Please guide me how can i do this properly using the way I've connected the database.
Thanks.
Though Pretty late, But we are doing something similar in a project. The entire journey looks like,
We have created all the SP's in individual SQL files and there is a deployment step, which inserts all the SP's into the database, fetching them from that particular location.
All the SP's basically return by running a SELECT
statement in the end of the SP. And not having a separate select outside the SP.
So a basic structure like
DELIMITER $$
DROP PROCEDURE IF EXISTS sp_copy_cleansheet;
$$
CREATE PROCEDURE `take_over_the_world` (#Some Input Args)
BEGIN
DECLARE method_to_be_used VARCHAR(1000);
# Define how to take over the world,
# SET method_to_be_used = "Approach A";
...
...
SELECT method_to_be_used as Result;
END
Now we call the SP's using sequelize raw queries,
const query = 'CALL take_over_the_world(:someArg)';
return Model.sequelize.query(query,
{
replacements : { someArg},
type : Model.sequelize.QueryTypes.SELECT
})
.then((response) => {
//Access response here. Its of the form [{Result: /*How to take over*/}]
});
It all seems to be working Perfectly so far :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With