Calling stored procedure in sequalize passing in a parameter

感情迁移 提交于 2019-12-10 11:40:03

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!