mysql - Calling Input/Output type Store Procedure in Sequelize -
i've created store procedure in mysql expect inputs , return output. call store procedure in mysql running
call createcoupon(1236,321, @message); select @message message
and getting output in message object.
now here comes situation need call sp in sequelize. i'm working on sailsjs project , using sequelize module queering.
i've created database connection in config/db_config , 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 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 need call sp in sequelize try:
var createcouponsql = "call createcoupon(1236,321, @message);"; createcouponsql += "select @message 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 sequlize trigger error:
executing (default): call createcoupon(1236,321, @message);select @message m essage error: error: er_parse_error: have error in sql syntax; check m anual corresponds mysql server version right syntax use near 'select @message message' @ line 1 @ query.sequence._packettoerror (c:\users\asd\desktop\cardcash p2\website\ node_modules\mysql\lib\protocol\sequences\sequence.js:30:14)
i've done r&d not able find thing proper call store procedure sending me response in @message , need execute select statement result.
please guide me how can using way i've connected database.
thanks.
though pretty late, doing similar in project. entire journey looks like,
we have created sp's in individual sql files , there deployment step, inserts sp's database, fetching them particular location.
all sp's return running select
statement in end of sp. , not having separate select outside sp.
so 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 take on world, # set method_to_be_used = "approach a"; ... ... select method_to_be_used result; end
now call 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. of form [{result: /*how take over*/}] });
it seems working far :)
Comments
Post a Comment