Skip to content

BigInt as Parameter works, but does this look right? #490

Closed

Description

@beautifulcoder

Hello, when I add a BigInt type as a parameter I get weird behavior.

Say:

commandBigInt.addParameter('Id', TYPES.BigInt, '9223372036854775807');

SQL Server tells me that a -9223372036854775808 was put in this parameter. This seems weird because it is a string type in JavaScript.

But, when I do this instead:

commandBigInt.addParameter('Id', TYPES.VarChar, '9223372036854775806');

It works! But, Tedious requires a VarChar type? I get that it has to be a string in JavaScript, but why in Tedious?

Activity

  1. tvrprasad commented on Jan 10, 2017

    @tvrprasad
    Contributor

    Can you please put together a complete script? I can take a look. Thanks.

  2. beautifulcoder commented on Jan 10, 2017

    @beautifulcoder
    Author

    You bet.

    Create a basic table with a BitInt.

    CREATE DATABASE FunWithBigIntInNode;
    GO

    USE FunWithBigIntInNode;
    GO

    CREATE TABLE dbo.BigIntInNodeTest (
    Id BIGINT NOT NULL,
    IsAtMax BIT NOT NULL,
    CONSTRAINT PK_BigIntInNodeTest_Id PRIMARY KEY(Id));
    GO

    Then, use this script:

    var Connection = require('tedious').Connection;
    var Request = require('tedious').Request;
    var TYPES = require('tedious').TYPES;
    var config = require('./sqlConfig');

    var connection = new Connection(config);

    var commandBigInt = new Request('dbo.InsertBigInt', function onRequest() {
    connection.close();
    });

    commandBigInt.addParameter('Id', TYPES.BigInt, '9223372036854775807');
    commandBigInt.addParameter('IsAtMax', TYPES.Bit, true);

    connection.on('connect', function onConnect() {
    connection.callProcedure(commandBigInt);
    });

    sqlConfig is a JSON key / value pair file, as such:

    {
    "userName": "",
    "password": "",
    "server": "",
    "options": {
    "database": "FunWithBigIntInNode",
    "encrypt": true
    }
    }

  3. tvrprasad commented on Feb 15, 2017

    @tvrprasad
    Contributor

    @beautifulcoder Got around to checking this out. This is essentially a JavaScript Numeric range limitation. You lose precision outside the range -(253 - 1) and (253 - 1). Using VarChar for values outside this range is the right behavior.

    I sent a PR to validate range on BigInt - #504.

  4. tvrprasad commented on Feb 21, 2017

    @tvrprasad
    Contributor

    @beautifulcoder This change is now merged. Happy coding :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions