2015-09-06 24 views
2

我在SQL Server 2012中有一个自定义的标量函数。它需要2个浮点数并返回一个nvarchar。经过测试并在SQL Server内部工作。如何使用mssql包为nodejs调用SQL Server标量函数?

我还没有找到任何如何从node-mssql调用它的例子。我所做的尝试失败:

TypeError: Cannot set property 'value' of undefined

这似乎是从节点繁琐的包错误...所以可能繁琐不支持它。

我有一个工作版本,调用一个存储过程与两个输入参数,返回一个记录集,但因为我真的只需要一个值,它似乎是在杀死,并且executeScalar调用将在这里有用(我的东西我曾经从.NET获得过)。

回答

4

您可以跳过node-mssql中的输入/输出设置并简单地选择标量函数,就像您通常那样。

功能

CREATE FUNCTION [dbo].[udfTestScalar] (
    -- Add the parameters for the function here 
    @F1 FLOAT 
    ,@F2 FLOAT 
    ) 
RETURNS NVARCHAR(100) 
AS 
BEGIN 
    -- Declare the return variable here 
    DECLARE @N1 NVARCHAR(100) 

    -- Add the T-SQL statements to compute the return value here 
    SET @N1 = N'Hi there' 

    RETURN @N1 
END 

index.js

var sql = require('mssql'); 

var config = { 
    user: 'user', 
    password: 'password', 
    server: 'localhost', 
    database: 'db' 
}; 

var connection = new sql.Connection(config, function(err) { 
    var request = new sql.Request(connection); 
    request.query("SELECT dbo.udfTestScalar(12345.123, 12345.456) as result", function(err, recordsets) { 
     console.log(recordsets[0].result); 
    }); 
}); 

输出

% node index.js 
'Hi there'