Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stored Procedures #16

Closed
scottchantry opened this issue Feb 15, 2012 · 8 comments
Closed

Stored Procedures #16

scottchantry opened this issue Feb 15, 2012 · 8 comments
Assignees

Comments

@scottchantry
Copy link

I don't know if you already have support for calling stored procedures. If you do, how do you call them? If not, would it take much effort to add support for stored procedures? Thanks!

@pekim
Copy link
Collaborator

pekim commented Feb 15, 2012

It should be possible to call stored procedures now, but in a quite limited way. If you don't need any output parameters, then you can use a Request to execute a statement that just happens to be a stored procedure.

For example, sp_who accepts no parameters, and outputs a result set. So it can be treated the same as the execution of any select statement.

Input parameters can passed using string concatention.

var sql = "exec my_proc @myParam = '" + value + "'";
new Request(sql, ...)

Of course this is horrible, and not a good strategy. Issue #14 will introduce support for execution of parameterised requests, which will remove the need for that approach. I intend to work on it soon, and expect it to be in the next release. It will only support a small number of data types for parameters intially.

Once parameterised requests are in place, it shouldn't require too much work to provide a decent API for execution of stored procedures.

@jschuhr
Copy link

jschuhr commented Mar 1, 2012

I don't mind running "exec my_proc" strings as SQL statements; it seems to work except the row count comes back undefined in these cases.

@pekim
Copy link
Collaborator

pekim commented Mar 18, 2012

Support for executing stored procedures, including passing input and output parameters has been added.
So far only tinyint, smallint, int, varchar and nvarchar are supported.

The documentation has not been updated yet.

@ReiGarcia
Copy link

Could you post a sample code to call a stored procedure using input/output parameters?
Thanks.

@pekim
Copy link
Collaborator

pekim commented Mar 31, 2012

var TYPES = require('tedious').TYPES;

var request = new Request('my_proc', function(err) {
    ....
});

var string = 'some text';
var number = 7;

request.addParameter(TYPES.VarChar, 'parameterName', string);
request.addParameter(TYPES.Int, 'anotherParameterName', number);
request.addOutputParameter(TYPES.VarChar, 'outputParameterName', {length: 10});    // varchar(10)

request.on('returnValue', function(parameterName, value, metadata) {
    console.log(parameterName + ' = ' + value);      // outputParameterName = ...
});

connection.callProcedure(request);

Note: I am not convinced that the API is quite right. There's a good chance that it will change.

@ReiGarcia
Copy link

Thanks!

@pekim
Copy link
Collaborator

pekim commented Apr 15, 2012

Please note, the first two arguments to the Request.addParameter and Request.addOutputParameter functions have been swapped around. The first argument is now the parameter name.

request.addParameter('parameterName', TYPES.VarChar, string);
request.addParameter('anotherParameterName', TYPES.Int, number);

@pekim
Copy link
Collaborator

pekim commented Apr 15, 2012

Included in v0.0.4.

@pekim pekim closed this as completed Apr 15, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants