我在SQL SERVER中手动创建了函数,然后使用Node.js中的Sequelize Library调用它们。
示例:
let query = "select * from getSomeData(:StartDate,:EndDate)";
let SeqOptions = {
replacements: {
StartDate : '2015-05-03',
EndDate : '2015-05-05',
},
type: sequelize.QueryTypes.SELECT
};
let result = await sequelize.query(query , SeqOptions );
但是我已经在SQL中手动定义了getSomeData
函数。是不是有办法在Sequelize中声明函数定义?
因此,每当我重新启动服务器并重置数据库时,我都不必在DB中运行脚本?
答案 0 :(得分:0)
您可以尝试此方法对我有用:
const procedureDropIfExists = `DROP FUNCTION IF EXISTS getSomeData;`
sequelize.query(procedureDropIfExists, {
type: sequelize.QueryTypes.raw
});
const procedureQuery = `CREATE FUNCTION getSomeData(arg1 INT, arg2 INT) RETURNS int(11)`
+ '\tBEGIN'
+ `\tDECLARE hold_result INT;`
+ `\tselect dummycolvalue INTO var_value from tableName whereClause;`
+ `\tRETURN hold_result;`
+ 'END;';
sequelize.query(procedureQuery, {
type: sequelize.QueryTypes.raw
});
让我知道它是否对您有用。