Skip to content
Advertisement

Few issues Inserting and filtering XML data to SQL

I’m having a few issues when inserting data from a XML file to a SQL Server database.

  1. How can I get the file name dynamically? This file is put on that path with a different number every time but always starts with Idoc and it’s a .xml file. I’m having troubles setting a variable inside OPENROWSET.

  2. At the moment I’m getting data from just one path (ZPROD) but I want to be able to insert data from EDI_DC40/DOCNUM instead of ZPROD/PLN_ORDER, I would need another CROSS APPLY but I can’t get it to work.

  3. I want to ignore the .000 from QTY, I tried setting it to int but it didn’t work, ‘can’t convert to int’.

  4. Is there a way to ignore most of LINENO and just get the last two characters? It’s set to varchar(2) but obviously is taking the first two characters, not the last ones.

Below is my code, I hope is all within the rules and understandable:

Thanks.

Advertisement

Answer

Please try the following solution.

  • No need to use the .query() method.
  • ##3,4 got resolved.
  • I took a liberty to handle invalid 240000 time as 23:59:59.
  • Last two columns data type is time(0).

SQL

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement