3

ストアド プロシージャと SQL スニペットの実行に非常に適した関数があります。Table Valued Parametersと同等の関数を作成するのを手伝ってくれる人がいるかどうか疑問に思っていましたか?

私の現在の機能は次のとおりです。

var exec = function (sql, callback) {
  var request = new Request(sql, function (err, rowCount) {
    if (err) {
      console.log("Statement failed: " + err);
    } else {
      console.log(rowCount + " rows");
    }

    callback(err, obj);
  });

  var obj = [];

  request.on("row", function (columns) {
    var values = {};
    columns.forEach(function (column) {
      if (column.isNull) {
        values[column.metadata.colName] = null;
      } else {
        values[column.metadata.colName] = column.value;
      }
    });
    obj.push(values);
    //console.log(obj);
    //console.log(" this is the obj in the row function");
  });
  conn.execSql(request);
  //console.log('obj: ' + obj);
  return obj;
};
function columnMetadata(columnsMetadata) {
  columnsMetadata.forEach(function (column) {
    //console.log(column);
  });
}

テーブルタイプを作成するためにSQLでこれを行いました:

CREATE TYPE dbo.inventoryRequestType AS TABLE
    (
    ItemNo int NULL,
    Qty int NULL,
    Department nvarchar(50) NULL,
    RequestedBy nvarchar(100) NULL
    )
GO

そして、このストアド プロシージャは、新しいテーブル タイプを使用して挿入するために使用されます。

CREATE PROCEDURE [dbo].[create_purchasing_list]
    -- Add the parameters for the stored procedure here
    @purchaseList inventoryRequestType READONLY

AS
BEGIN
    SET NOCOUNT ON;
    INSERT INTO [Inventory Requests]
        (
        ItemNo,
        Qty,
        Department,
        RequestedBy
        )
    SELECT
        ItemNo,
        Qty,
        Department,
        RequestedBy
    FROM @purchaseList
END

SQL では、以下を使用してストアド プロシージャを実行できます。

DECLARE @purchaseList inventoryRequestType

INSERT INTO @purchaseList

SELECT 4444, 1, 'layup', 'marc'

EXEC Purchasing.dbo.create_purchasing_list @purchaseList

これは私が迷子になるところです。この最後の部分をnode/tediousで機能させる方法がわかりません。

これは、助けが必要な関数でテーブルを定義する方法の例です。Columns変数は、Rowsそれを含む関数によって定義されます。

var table = {
  columns: [
    // Columns
    {name: 'id', type: Types.Int},
    {name: 'firstName', type: Types.nvarchar, length: 50},
    {name: 'option', type: Types.Bit}
  ],
  rows: [
    // Rows
    [15, 'Bob', true],
    [16, 'Rob', false]
  ],
};
4

3 に答える 3

2

これで、完全に機能するソリューションができました。非常に動的なので、ここの例で解決策を見つけることができなかったので共有したいと思います.

これは、 app.jsによって呼び出されるdb-functions.jsファイルです。

var express = require("express");
var tds = require("tedious");
var Request = require("tedious").Request;
var Types = require("tedious").TYPES;
var ConnectionPool = require("tedious-connection-pool");

var poolConfig = {
  min: 10,
  log: true,
};

var config = {
  userName: "User",
  password: "password",
  server: "192.168.1.123",
  options: {
    requestTimeout: 30 * 1000,
    instanceName: "InstanceOnServer",
    //instanceName: '\SQLServer', // Name of SQL Instance
    rowCollectionOnRequestCompletion: true,
    database: "PlantDB",
    encrypt: true,
    debug: {
      data: true, // lots of info generated in console when true
      payload: false,
      token: false,
      packet: true, // was on true
      log: true,
    },
  },
};

var pool = new ConnectionPool(poolConfig, config); //multiple connections

pool.acquire(function (err, connection) {
  if (err) {
    console.log(err);
  } else {
    console.log("connected");
  }
});

//var conn = new tds.Connection(config);

pool.on("error", function (err) {
  console.log(err);
});

var conn = new tds.Connection(Config);

function requestDone(rowCount, more) {
  console.log(rowCount + "rows------");
  console.log(column);
}

function infoError(info) {
  console.log(info.number + " : " + info.message);
}

function debug(message) {
  console.log(message);
}

var exec = function (sql, callback) {
  // use this function to execute SELECT statements and Stored Procedures

  var request = new Request(sql, function (err, rowCount) {
    if (err) {
      console.log("Statement failed: " + err);
    } else {
      console.log(rowCount + " rows");
    }

    callback(err, obj);
  });

  var obj = []; //holds the data generated by the SQL statement when applicable

  request.on("row", function (columns) {
    //identify individual rows
    var values = {}; //insert a value
    columns.forEach(function (column) {
      //identify the column
      if (column.isNull) {
        /*adds a key to the value so that each item in
                           the object is broken into "Column: Value" pairs*/
        values[column.metadata.colName] = null;
      } else {
        values[column.metadata.colName] = column.value;
      }
    });
    obj.push(values);
  });
  conn.execSql(request);
  //console.log('obj: ' + obj);
  return obj;
  function columnMetadata(columnsMetadata) {
    columnsMetadata.forEach(function (column) {
      //console.log(column);
    });
  }
};

var storedProcedures = {
  //This is an actual stored procedure that exists in my
  purchasing_list:
    //Purchases Database which is used to identify this object
    {
      fullName: "Purchases.dbo.purchasing_list",
      //fullname of stored procedure
      variable: "purchaseList",
      //the name of my "Table Type" Variable created in SQL
      Columns: [
        { name: "ItemNo", type: Types.Int },
        { name: "ProductName", type: Types.NVarChar },
        { name: "ProductDescription", type: Types.NVarChar },
        { name: "UnitOfMeasure", type: Types.NVarChar },
        { name: "Qty", type: Types.Int },
        { name: "Department", type: Types.NVarChar },
        { name: "RequestedBy", type: Types.NVarChar },
      ],
    },
  another_stored_procedure: {
    fullName: "Purchases.dbo.another_stored_procedure",
    variable: "purchaseList",
    Columns: [
      { name: "ItemNo", type: Types.Int },
      { name: "ProductName", type: Types.NVarChar },
      { name: "ProductDescription", type: Types.NVarChar },
      { name: "UnitOfMeasure", type: Types.NVarChar },
      { name: "Qty", type: Types.Int },
      { name: "Department", type: Types.NVarChar },
      { name: "RequestedBy", type: Types.NVarChar },
    ],
  },
};

var callProc = function (procedureName, Rows, callback) {
  var usefulData = storedProcedures[procedureName];
  var message = "";

  var table = {
    columns: usefulData["Columns"],
    rows: Rows,
    //insert an array of arrays with values defined by storedProcedure
  };

  var request = new Request(usefulData["fullName"], function (err, rowCount) {
    if (err) {
      console.log("Statement failed: " + err);
      message = "There was a problem submitting your request!";
    } else {
      console.log(rowCount + "No errors in TVP");
      //this will likely return an undefined rowCount since this function
      //is generally used to insert and update
      message = "Success";
    }
    callback(err, message);
  });

  request.on("doneProc", function (rowCount, more, returnStatus, rows) {
    //console.log('Row Count' + rowCount);
    //console.log('More? ' + more);
    //console.log('Return Status: ' + returnStatus);
    //console.log('Rows:' + rows);
  });

  request.addParameter(usefulData["variable"], Types.TVP, table);
  //Add a table type set of parameters to the request to complete the
  //transaction

  conn.callProcedure(request);
};

var insert = function (sql, callback) {
  var request = new Request(sql, function (err, rowCount) {
    if (err) {
      console.log("Statement failed: " + err);
      callback = err;
    } else {
      console.log("Insert Statement succeeded");
      callback = "Success";
    }
  });

  conn.execSql(request);
};

var update = function (sql, callback) {
  var insert = "";
  var request = new Request(sql, function (err, rowCount) {
    if (err) {
      insert = "Statement failed: " + err;
    } else {
      insert = "Insert Statement succeeded";
    }
    callback(err, insert);
  });

  conn.execSql(request);
};

exports.conn = conn;
exports.exec = exec;
exports.callProc = callProc;
exports.insert = insert;
exports.update = update;

App.jsファイル内で、次のようにデータベースをロードしました。

var db = require('./public/javascripts/db-functions');

複数の行を挿入または更新できるcallProcedureは、次のように実行できます。

app.route("/update_purchasing").post(function (req, res) {
  var Rows = req.body.Rows;

  db.callProc("create_purchasing_list", Rows, function (err, success) {
    if (err) {
      res.json("Error creating purchasing list: " + err);
    } else {
      res.json("Successfully updated your purchase list");
    }
  });
});

は、ストアド プロシージャで選択された列に基づいて、次のようになります。

var Rows = [
  [0101, "hair gel", "makes your hair stick up", "vat", 1, "house", "Bob"],
  [0102, "carbon", "makes light boats", "roll", 1, "Layup", "Rob"],
];

EXEC my lookを使用してストアド プロシージャを実行する方法の例は、次のようになります。

app.get("/start", function (req, res) {
  var sess = req.session;

  db.exec(
    "dbo.populate_shipping_schedule" + "parameter1",
    function (err, schedule) {
      if (!err) {
        sess.schedule = schedule;
        res.render("start", { title: "Start" });
      } else {
        console.log("This is an Error: " + err);
      }
    }
  );
});

于 2016-01-08T19:06:43.430 に答える
1
`var storedProcedures = {
'purchasing_list':
    {
        'fullName': 'Purchases.dbo.purchasing_list',
        'variable': 'purchaseList',
        'Columns' : [
            { 'name': 'ItemNo', 'type': Types.Int }
            , { 'name': 'ProductName', 'type': Types.NVarChar }
            , { 'name': 'ProductDescription', 'type': Types.NVarChar }
            , { 'name': 'UnitOfMeasure', 'type': Types.NVarChar }
            , { 'name': 'Qty', 'type': Types.Int }
            , { 'name': 'Department', 'type': Types.NVarChar }
            , { 'name': 'RequestedBy', 'type': Types.NVarChar }
        ]
    }
}

var callProc = function (procedureName, Rows,callback) {

var usefulData = storedProcedures[procedureName];

var table = {
    columns: usefulData['Columns'],
    rows: Rows          //insert an array of arrays with values defined by storedProcedure
};

var request = new Request(usefulData['fullName'], function (err, rowCount) {
    if (err) {
        console.log('Statement failed: ' + err);
        callback = err;
    } else {
        console.log('No errors in TVP')
        callback = 'Success';
    }

});

request.addParameter(usefulData['variable'], Types.TVP, table);

conn.callProcedure(request);

};`
于 2016-01-08T16:18:49.073 に答える