2017-08-15 7 views
0

私はSqlサーバを使用しています。node.jsには、特定のテーブルに新しい行が挿入されるたびに実行したい機能があります。Sqlサーバに挿入時にnode.js関数を呼び出す

これは、テーブルに変更が加えられたときに実行したいコードです。

private sql = require('mssql'); 
private currentID: any; 

`private config = { 
user: 'sa', 
password: 'sa', 
server: '192.168.100.2', 
port: '1433', 
database: 'test1', 
connectionTimeout: 100000,}; 
` 
public checkDatabase() { 
    this.sql.connect(this.config).then((connection: any) => { 
     new this.sql.Request(connection).query('SELECT EventID FROM Events ORDER BY EventID DESC').then((recordset: any) => { 

      console.log(recordset.recordset[0].EventID); 
      if (this.currentID == recordset.recordset[0].EventID) { 
       console.log("They are the same " + this.currentID + " " + recordset.recordset[0].EventID); 
      } 
      else { 
       console.log("Not the same " + this.currentID + " " + recordset.recordset[0].EventID); 
      } 
      this.currentID = recordset.recordset[0].EventID; 
     }); 
    }); 
} 

お願いします。

+0

あなたがすべきinsert triggerを作成する –

+0

一般的に不可能です。行がアプリケーションから挿入された場合、通知を受ける方法はありません。あなたはすべての保存関数のためのデコレータを作成しようとすることができます。 – StanislavL

+0

@JúliusMarkoあなたは私に詳細を教えてください、私は多くのトリガーについてはよくわかりません – DavidS

答えて

0

一般的な方法は、挿入操作のためのトリガーを作成することです - 与えられたコードは、指定したテーブル内のすべての挿入後に実行されることを意味します

CREATE TRIGGER name_of_your_trigger 
ON test1.Events 
AFTER INSERT 
AS 
    /* here you should specify what do you want to do, for instance print something: */ 
    print "Hello world" 
GO 

は、詳細はドキュメントを参照してください。https://docs.microsoft.com/en-us/sql/t-sql/statements/create-trigger-transact-sql

関連する問題