问题
I'm trying to call a stored procedure passing in a parameter as shown below. However, I know my syntax is off and I don't see any examples doing this, either in the docs or unit tests.
model.sequelize.query('CALL truncate_tables(\'appuser\');')
.then(function (response) {
done();
}).error(function (err) {
done(err);
});
suggestions?
I started off looking at this link. Calling stored procedures in Sequelize.js
Additional info:
here is the error I am seeing
Unhandled rejection SequelizeDatabaseError: syntax error at or near "CALL"
and here is the sp
CREATE OR REPLACE FUNCTION truncate_tables(username character varying)
RETURNS void AS
$BODY$
DECLARE
statements CURSOR FOR
SELECT tablename FROM pg_tables
WHERE tableowner = username AND schemaname = 'public';
BEGIN
FOR stmt IN statements LOOP
EXECUTE 'TRUNCATE TABLE ' || quote_ident(stmt.tablename) || ' CASCADE;';
END LOOP;
END;
$BODY$
回答1:
I suppose you are using PostgreSQL.
PostgreSQL calls procedures with a syntax which is different from the other DBMS like MySQL or IBM Informix. It does not CALL the procedure but instead SELECTs from the stored procedure. A reference may be found at https://www.postgresql.org/message-id/41F009EA.6050401%40us.michelin.com.
Maybe this code would thus work:
model.sequelize.query('SELECT truncate_tables(\'appuser\');')
.then(function (response) {
done();
}).error(function (err) {
done(err);
});
来源:https://stackoverflow.com/questions/38088830/calling-stored-procedure-in-sequalize-passing-in-a-parameter