Hi n8n Community
Please help this newbie
I am trying to receive an order via API and write the received data into a table in Microsoft SQL Server.
I couldn’t use insert mode, it didn’t work at all, so I am executing a script instead.
this script works fine because the id is a simple value:
INSERT INTO Customers.Deleted(id)
VALUES(’{{$node["$json[“id”]}}’);
But this script won’t work for inserting line_items array as NVARCHAR into a column
the inserted value for line_items is [object Object],[object Object],[object Object]
INSERT INTO [Orders].[Created]
([id],
[line_items]
)
VALUES
(’{{$json[“id”]}}’,
‘{{$json[“line_items”]}}’
);
Two questions.
1- is there any fix for this script? how can I have the whole array inserted into line_items column?
2- is it possible (for example by using a function) to insert each line item from line items to a second table? I mean ne table for the order header and second table for line items, how can I do this?
Any suggestion or help will be highly appreciated.
Thanks
Mike
Hey @intelioum
As far as I can see, there seems to be a problem with the incoming data you’re getting to your MSSQL node.
Have you tried using a set node in front of it to actually fix the data? This would allow you to use the insert node correctly.
I have a sample workflow that I use for testing and it’s working fine. Please have a look here:
PS: if you’re not sure what to do, you can copy this JSON, go to n8n, create a new blank workflow and paste it (CTRL + V). You should be able to see it.
I hope this helps!
@krynble
What you mentioned is a very basic SQL usage but this is not what I am facing.
When I am executing the SQL Server node it shows the arrays in the result and this what supposed to be inserted in the table
But when you look at the inserted data this is what I get
It seems that this is a bug in SQL Server node.
it passes the single string but can’t pass an Array to the SQL Table.
Same thing with Postgres works fine with no issue.
What I am trying to do as a work around is to have a SET node or a function to extracts just that array from the whole data and INSERT it to a new table.
SO, basically, how I can split a large array and just transfer a sub-array to the next node?
I think SET is the answer but I can’t get it to work.
@intelioum
Thank you for the feedback! Could you share your table schema (a create table with similar data is enough) and some testing data so I can test and fix?
I saw that the node code (Can be found here) does not work properly with Arrays. This is probably the cause.