Tôi đã tạo một quy trình lưu trữ trong MySQL mà mong đợi một số đầu vào và trả về một số đầu ra. Để gọi thủ tục lưu trữ trong MySQL tôi đang chạyGọi loại đầu vào/đầu ra Quy trình lưu trữ trong phần tiếp theo
CALL createCoupon(1236,321, @message);
SELECT @message AS message
và nhận đầu ra trong đối tượng tin nhắn.
bây giờ ở đây có tình huống tôi cần gọi SP này trong phần tiếp theo. Tôi đang làm việc trên dự án sailsjs và sử dụng mô-đun sequelize để xếp hàng.
tôi đã tạo ra các kết nối cơ sở dữ liệu trong config/db_config, chuỗi kết nối của tôi là:
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
})
và tôi gọi đó là trong điều khiển như:
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){
})
}
}
bây giờ tôi cần phải gọi SP trong sequelize vì vậy tôi chỉ đơn giản là cố gắng:
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);
});
nhưng sequlize kích hoạt các lỗi:
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)
tôi đã thực hiện một số R & D nhưng không thể tìm thấy bất cứ điều gì thích hợp để gọi một thủ tục lưu trữ được gửi cho tôi trở lại các phản ứng trong @message và tôi cần phải thực hiện một tuyên bố chọn để có được kết quả .
Vui lòng hướng dẫn tôi làm cách nào tôi có thể thực hiện việc này đúng cách bằng cách tôi đã kết nối cơ sở dữ liệu.
Cảm ơn.
tôi nghĩ rằng bạn có thể gọi hai truy vấn thô một cách riêng biệt và nó sẽ hiển thị cho bạn kết quả chính xác nhưng đây không phải là cách thích hợp. –