UNPKG

node-red-contrib-mssql-plus

Version:

A node-red node to execute queries, stored procedures and bulk inserts in Microsoft SQL Server and Azure Databases SQL2000 ~ SQL2022

504 lines 15.9 kB
[ { "id": "3eb71b1f.47fdb4", "type": "MSSQL", "z": "403d67ed1c8bce92", "mssqlCN": "8d9d212a.cea03", "name": "", "outField": "payload", "returnType": "1", "throwErrors": "1", "query": "MyCustomStoredProcedure", "modeOpt": "", "modeOptType": "execute", "queryOpt": "", "queryOptType": "editor", "paramsOpt": "", "paramsOptType": "editor", "rows": "rows", "rowsType": "msg", "parseMustache": true, "params": [ { "output": false, "name": "tvp", "type": "TVP", "valueType": "msg", "value": "table", "options": { "nullable": true, "primary": false, "identity": false, "readOnly": false } } ], "x": 1100, "y": 700, "wires": [ [ "bfde3540.a91018" ] ] }, { "id": "614325ad.afd22c", "type": "inject", "z": "403d67ed1c8bce92", "name": "3. Execute Procedure", "props": [ { "p": "payload" }, { "p": "topic", "vt": "str" } ], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "[[\"I am sent from an inject node\",444],[\"then rendered in the static parameter\",555]]", "payloadType": "json", "x": 600, "y": 700, "wires": [ [ "63fa0f60.56474" ] ] }, { "id": "bfde3540.a91018", "type": "debug", "z": "403d67ed1c8bce92", "name": "", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "true", "targetType": "full", "x": 1250, "y": 700, "wires": [] }, { "id": "e94800fc.ccd07", "type": "comment", "z": "403d67ed1c8bce92", "name": "Read me - using TVP (Static query and param type set in the MSSQL node, param / Dynamic param value)", "info": "Using the example sql type and procedure [here](https://tediousjs.github.io/node-mssql/#table-valued-parameter-tvp), you would need data in the value to be in the following format...\n```json\n{\n \"columns\": [\n {\n \"name\": \"a\",\n \"type\": \"VarChar(50)\"\n },\n {\n \"name\": \"b\",\n \"type\": \"Int\"\n }\n ],\n \"rows\": [\n [ \"hello tvp\", 777 ],\n [ \"bye tvp\", 888 ],\n [ \"call the cops\", 999 ]\n ]\n}\n```\nthis flow works with the above example...", "x": 840, "y": 660, "wires": [] }, { "id": "31c4ef57.ffa84", "type": "comment", "z": "403d67ed1c8bce92", "name": "Read me - setup SQL Connection", "info": "1. Open the MSSQL node\n2. Click the [pencil] icon to edit the connection\n3. Enter your servers host name (or IP), username, password, database name\n4. Click Done, Done\n5. cycle through the 4 numbered Inject nodes.\n\n", "x": 610, "y": 260, "wires": [] }, { "id": "762d8c3.81ea874", "type": "MSSQL", "z": "403d67ed1c8bce92", "mssqlCN": "8d9d212a.cea03", "name": "", "outField": "payload", "returnType": "1", "throwErrors": "1", "query": "", "modeOpt": "", "modeOptType": "query", "queryOpt": "payload", "queryOptType": "msg", "paramsOpt": "", "paramsOptType": "editor", "rows": "rows", "rowsType": "msg", "parseMustache": true, "params": [], "x": 1100, "y": 460, "wires": [ [ "96087d50.fe40d" ] ] }, { "id": "212bac4b.281e64", "type": "inject", "z": "403d67ed1c8bce92", "name": "1. click me", "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "", "payloadType": "date", "x": 560, "y": 420, "wires": [ [ "3342797c.efa666" ] ] }, { "id": "3342797c.efa666", "type": "template", "z": "403d67ed1c8bce92", "name": "create TYPE", "field": "payload", "fieldType": "msg", "format": "sql", "syntax": "mustache", "template": "CREATE TYPE TestType AS TABLE ( a VARCHAR(50), b INT );\n", "output": "str", "x": 790, "y": 420, "wires": [ [ "762d8c3.81ea874" ] ] }, { "id": "8885e5af.51be18", "type": "inject", "z": "403d67ed1c8bce92", "name": "4. click me", "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "", "payloadType": "date", "x": 560, "y": 500, "wires": [ [ "6783b62a.0ad9f8" ] ] }, { "id": "6783b62a.0ad9f8", "type": "template", "z": "403d67ed1c8bce92", "name": "drop TYPE and PROCEDURE", "field": "payload", "fieldType": "msg", "format": "sql", "syntax": "mustache", "template": "\n\nDROP PROCEDURE MyCustomStoredProcedure;\n\nDROP TYPE TestType;", "output": "str", "x": 850, "y": 500, "wires": [ [ "762d8c3.81ea874" ] ] }, { "id": "f9848462.001d58", "type": "inject", "z": "403d67ed1c8bce92", "name": "2. click me", "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "", "payloadType": "date", "x": 560, "y": 460, "wires": [ [ "757512fd.70849c" ] ] }, { "id": "757512fd.70849c", "type": "template", "z": "403d67ed1c8bce92", "name": "create PROCEDURE", "field": "payload", "fieldType": "msg", "format": "sql", "syntax": "mustache", "template": "CREATE PROCEDURE MyCustomStoredProcedure (@tvp TestType readonly) AS SELECT * FROM @tvp;\n", "output": "str", "x": 820, "y": 460, "wires": [ [ "762d8c3.81ea874" ] ] }, { "id": "96087d50.fe40d", "type": "debug", "z": "403d67ed1c8bce92", "name": "", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "true", "targetType": "full", "x": 1250, "y": 460, "wires": [] }, { "id": "8d138f11.b2875", "type": "comment", "z": "403d67ed1c8bce92", "name": "Read me - Setup", "info": "1 - create the type in the database\n2 - create the procedure in the database\n4 - drop the procedure and type from the database", "x": 560, "y": 380, "wires": [] }, { "id": "63fa0f60.56474", "type": "function", "z": "403d67ed1c8bce92", "name": "create table for 'tvp' param", "func": "var table = {\n \"columns\":[\n {\"name\":\"a\",\"type\":\"VarChar(50)\"},\n {\"name\":\"b\",\"type\":\"Int\"}\n ],\n \"rows\": msg.payload //get rows from payload of prev node\n}\nmsg.table = table;\nreturn msg;", "outputs": 1, "noerr": 0, "initialize": "", "finalize": "", "x": 840, "y": 700, "wires": [ [ "3eb71b1f.47fdb4" ] ] }, { "id": "560be5fd.e4826c", "type": "comment", "z": "403d67ed1c8bce92", "name": "Read me - using TVP (Fully Dynamic (MSSQL node has nothing set other than the connection))", "info": "Using the example sql type and procedure [here](https://tediousjs.github.io/node-mssql/#table-valued-parameter-tvp), you would need data in the value to be in the following format...\n```json\n{\n \"columns\": [\n {\n \"name\": \"a\",\n \"type\": \"VarChar(50)\"\n },\n {\n \"name\": \"b\",\n \"type\": \"Int\"\n }\n ],\n \"rows\": [\n [ \"hello tvp\", 777 ],\n [ \"bye tvp\", 888 ],\n [ \"call the cops\", 999 ]\n ]\n}\n```\n\nThe inject node provides the raw data values in the form of an array of rows (which are arrays os value) e.g...\n```\n[ \n [val1, val2], //row 1\n [val1, val2], //row 2\n]\n```\n\nIn the function node, we add more values for the MSSQL node...\n* `msg.payload` is set to the name of the stored procedure\n* `msg.queryMode` is set to 'execute' (so that the stored procedure node is used)\n* `msg.queryParams` is set to an array of parameter objects\n\n", "x": 810, "y": 760, "wires": [] }, { "id": "cf651a84.3e0048", "type": "inject", "z": "403d67ed1c8bce92", "name": "3. Execute Procedure", "props": [ { "p": "payload" }, { "p": "topic", "vt": "str" } ], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "[[\"I am just an array value...\",777],[\"I was sent in from an inject node\",888],[\"but I could come from anywhere\",999]]", "payloadType": "json", "x": 600, "y": 800, "wires": [ [ "b5125a59.44c0e8" ] ] }, { "id": "b5125a59.44c0e8", "type": "function", "z": "403d67ed1c8bce92", "name": "dynamic query, table and params", "func": "\n//create the TVP data table parameter value\nvar table = {\n \"columns\":[\n {\"name\":\"a\",\"type\":\"VarChar(50)\"},\n {\"name\":\"b\",\"type\":\"Int\"}\n ],\n \"rows\": msg.payload //get rows from payload of prev node\n}\n\n//set query mode to execute a stored procedure\nmsg.queryMode = \"execute\";\n\n//set the query in payload \nmsg.query = \"MyCustomStoredProcedure\";//set the query to name of the stored procedure to use\n\n//setup the params array \nmsg.queryParams = [];\n\n//add param\nmsg.queryParams.push({\n \"name\": \"tvp\",\n \"type\": \"TVP\", //can also use \"TVP(TestType)\"\n \"value\": table,\n \"output\": false\n});\n\nreturn msg;", "outputs": 1, "timeout": "", "noerr": 0, "initialize": "", "finalize": "", "libs": [], "x": 860, "y": 800, "wires": [ [ "f3a59f13.b2201" ] ] }, { "id": "f3a59f13.b2201", "type": "MSSQL", "z": "403d67ed1c8bce92", "mssqlCN": "8d9d212a.cea03", "name": "", "outField": "payload", "returnType": "1", "throwErrors": "1", "query": "", "modeOpt": "queryMode", "modeOptType": "msg", "queryOpt": "query", "queryOptType": "msg", "paramsOpt": "queryParams", "paramsOptType": "msg", "rows": "rows", "rowsType": "msg", "parseMustache": true, "params": [], "x": 1100, "y": 800, "wires": [ [ "971095a3.b7d678" ] ] }, { "id": "971095a3.b7d678", "type": "debug", "z": "403d67ed1c8bce92", "name": "", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "true", "targetType": "full", "x": 1250, "y": 800, "wires": [] }, { "id": "c01c04cb.fec838", "type": "MSSQL", "z": "403d67ed1c8bce92", "mssqlCN": "8d9d212a.cea03", "name": "", "outField": "payload", "returnType": "1", "throwErrors": "1", "query": "MyCustomStoredProcedure", "modeOpt": "", "modeOptType": "execute", "queryOpt": "", "queryOptType": "editor", "paramsOpt": "", "paramsOptType": "editor", "rows": "rows", "rowsType": "msg", "parseMustache": true, "params": [ { "output": false, "name": "tvp", "type": "TVP", "valueType": "json", "value": "{\"columns\":[{\"name\":\"a\",\"type\":\"VarChar(50)\"},{\"name\":\"b\",\"type\":\"Int\"}],\"rows\":[[\"i am hard coded in MSSQL Node\",111],[\"me too too too\",222],[\"me three three three\",333]]}", "options": { "nullable": true, "primary": false, "identity": false, "readOnly": false } } ], "x": 1100, "y": 600, "wires": [ [ "25357ef9.64e812" ] ] }, { "id": "996c5863.3607e8", "type": "inject", "z": "403d67ed1c8bce92", "name": "3. Execute Procedure", "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "true", "payloadType": "bool", "x": 600, "y": 600, "wires": [ [ "c01c04cb.fec838" ] ] }, { "id": "25357ef9.64e812", "type": "debug", "z": "403d67ed1c8bce92", "name": "", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "true", "targetType": "full", "x": 1250, "y": 600, "wires": [] }, { "id": "8d30cc63.4638e", "type": "comment", "z": "403d67ed1c8bce92", "name": "Read me - using TVP (Static setup of query, param and param value in the MSSQL node)", "info": "Using the example sql type and procedure [here](https://tediousjs.github.io/node-mssql/#table-valued-parameter-tvp), you would need data in the value to be in the following format...\n```json\n{\n \"columns\": [\n {\n \"name\": \"a\",\n \"type\": \"VarChar(50)\"\n },\n {\n \"name\": \"b\",\n \"type\": \"Int\"\n }\n ],\n \"rows\": [\n [ \"hello tvp\", 777 ],\n [ \"bye tvp\", 888 ],\n [ \"call the cops\", 999 ]\n ]\n}\n```\nthis flow works with the above example...", "x": 790, "y": 560, "wires": [] }, { "id": "8d9d212a.cea03", "type": "MSSQL-CN", "tdsVersion": "7_4", "name": "My SQL Server connection", "server": "172.17.1.2", "port": "1433", "encyption": true, "trustServerCertificate": true, "database": "testdb", "useUTC": true, "connectTimeout": "15000", "requestTimeout": "15000", "cancelTimeout": "5000", "pool": "5", "parseJSON": false, "enableArithAbort": true, "readOnlyIntent": false } ]