如何在Sequelize中使用输出参数在SQL Server中执行存储过程?

时间:2019-02-24 17:45:02

标签: node.js sql-server sequelize.js

尝试在Sequelize中使用输出参数执行存储过程时出现错误:

Executing (default): spsPerson @PersonID=0 OUTPUT, @Firstname=N'test', @Lastname=N'test'
  

UnhandledPromiseRejection警告:SequelizeDatabaseError:将常量传递给存储过程时无法使用OUTPUT选项。

这是代码:

  seq.query(
    'spsPerson @PersonID=:PersonID OUTPUT, @Firstname=:firstname, @Lastname=:lastname,
    {
      type: seq.QueryTypes.SELECT,
      replacements: {
        PersonID: 0,
        Firstname: 'test',
        Lastname: 'test'
      },
    }
  );

这是我的存储过程:

SET ANSI_NULLS OFF
GO

SET QUOTED_IDENTIFIER OFF
GO

CREATE PROCEDURE [dbo].[spsPerson]
    @PersonID INT OUTPUT,
    @Firstname VARCHAR(50),
    @Lastname VARCHAR(255)
AS
    INSERT INTO PersonTable (Firstname,  Lastname)
    VALUES (@Firstname,  @Lastname)

    SET  @PersonID  = SCOPE_IDENTITY()
GO

我不在乎输出参数,只想执行此存储过程而不更改它。

2 个答案:

答案 0 :(得分:0)

问题出在OUTPUT参数上,您正在为其分配一个值0,这引起了问题。

这是在TSQL中执行它以将结果获取到变量@results的方式:

declare @results int;
exec dbo.spsPerson @results OUTPUT, @Firstname=N'test', @Lastname=N'test'
select @results;

如果您根本不关心结果:

exec dbo.spsPerson NULL, @Firstname=N'test', @Lastname=N'test'

答案 1 :(得分:0)

我暂时不测试。你会尝试并让我知道吗

定义存储功能:

SET ANSI_NULLS OFF
GO

SET QUOTED_IDENTIFIER OFF
GO

CREATE PROCEDURE [dbo].[spsPerson]
    @Firstname VARCHAR(50),
    @Lastname VARCHAR(255)
AS
    INSERT INTO PersonTable (Firstname,  Lastname)
    VALUES (@Firstname,  @Lastname)

    return (SELECT IDENT_CURRENT('PersonTable'))
GO

连续查询:

sequelize
          .query('CALL spsPerson (:Firstname, :Lastname)', 
                {replacements: { Firstname: 'fistname', Lastname: 'lastname' }})
          .then(v=>console.log(v));