- SQL Server csv in xml out Project
- SQL Server csv in xml out Project Part 2
- SQL Server csv in xml out Project Part 3
- SQL Server csv in xml out Project Part 4
- SQL Server csv in xml out Project Part 5
- SQL Server csv in xml out Project Part 6
- SQL Server csv in xml out Project Part 7
- SQL Server csv in xml out Project Part 8
- SQL Server csv in xml out Project Part 9
- SQL Server csv in xml out Project Part 10
- SQL Server csv in xml out Project Part 11
- SQL Server csv in xml out Project Part 12
- SQL Server Move Data to a File Part 13
Using SQL Server we will design a project where we process an incoming csv file and prepare an xml file for output.
We will be working for a fictitious company called I Love Books Inc. We will create a new SQL Server database called [ILoveBooks] to do this. The files contain books that we have brought into the retail store as inventory. The company purchases books from different suppliers. All of the suppliers will use exactly the same format of csv file to record the books sent to I Love Books Inc. This is because when we asked them, they agreed to do so. The output xml file will contain the same data as the input file, plus a bit more information. It will be consumed by I Love Books Inc.’s accounting system. We don’t need to worry about how the accounting system imports the xml file.
Our job is to design an algorithm that will solve this challenge. First though we will start with a list of considerations that will be the beginnings of formulating the Requirements and Specifications.
- A folder for the incoming csv files and a naming convention
- A specific file called inbooks.csv in a specific folder will be SQL Server’s data source
- A folder for the outgoing xml files and a naming convention
- The entire process of importing and exporting is all-or-nothing
- We will take control of error trapping and provide good error reporting
- May or may not record all errors to an error log table
- Import the csv file and save data as a permanent record
- Incoming data must be validated and cleaned if unclean
- As part of validation, there can be no duplicates inside of the incoming file itself
- The incoming file cannot be stored in database more than once (duplicates will result)
- A table containing a list of valid suppliers is stored in the database
- The csv file cannot be imported unless the supplier is found in the [Suppliers] table
- Our output xml file must be validated against an xml schema (XSD)
- A permanent record of all xml files produced are stored in the database
Technical Specifications – Initial Thoughts
This section will explore some ideas as to how we will accomplish some of the above requirements.
- Date and time stamp the csv file name. The file name will include the supplier’s company code name followed by a date and time stamp in the format “_YYYYMMDDHHMMSS. There will be one header line , at least one detail line and one trailer line in the csv file. The folder will be named C:\data\ilovebooks\incoming\pending
- Date and time stamp the xml file name. The file name will include our company name “ilovebooks” followed by “_YYYYMMDDHHMMSS.xml”. There will be a header, detail and trailer sections to the csv file. The folder will be named C:\data\ilovebooks\incoming
- SQL Server Transactions and Stored Procedures
- We will use TRY…CATCH blocks
- We may not implement logging
- We need “history” tables to append to each time we import. Transactions will roll it back if there is a critical error. The data will come from the incoming tables. The data moves from the csv file to the incoming tables to the history tables. After the incoming data is cleaned and validated it can be copied to the “history” tables.
- Validate the supplier code and the date-time in the header against the file name itself. They must match or abort the whole process. the to ensure it matches with the company name in the [Suppliers] table that holds a list of all of the suppliers. This [Suppliers] table has columns for [FullSupplierName], [SupplierCode] and [datetimeadded] to table, with the [SupplierCode] as a primary key. No columns may contain NULL. All of the columns in the incoming csv must exist in the correct order and must contain data, even if empty strings.
- The ISBN number is a primary key, so we will get an error from SQL Server if it is a violation, so we do not need to check this one.
- We could store the xml in tables and in an xml data type
At this point we could begin designing our stored procedures. We will have one stored procedure that calls (EXEC) the other stored procedures. There are benefits to breaking down code into distinct modules. How will we structure it? What are our naming conventions for our user-defined stored procedures. One convention is to start all of them with “usp_”.
- First Stage
- Second Stage
- Third Stage
- Fourth Stage
- Fifth Stage
If we are happy with our structure shown above, we could proceed to create all of the stored procedures. We also know that each of these stored procedures are using transactions and TRY…CATCH blocks. Since this is “boilerplate code, we could simply copy the code into each stored procedure. Since our code is well-structured, we can add more stored procedures if we need to. Since we are ordering the stored procedures with numbers, they will sort well in SSMS.
We also can map these procedures to the previous posts at this site or at other sites so that when you are beginning to code the stored procedure, you can refer to the code you need and modify it to suit your needs.