Dynamically Generate Format File For OPENROWSET
By Joey Moelands, 2016/04/18
I was wondering why nobody ever wrote a script to dynamically generate a format file for BCP based on meta data. When you store youre target table and field definitions in meta data.
In steps (you need to program it your selve, don’t want to make it to easy…)
1. Fill the meta data tables with your table definition
2. Create stored procedure for drop / create table from meta data definition
3. Create stored procedure for Select … From table from meta data definition
And now the magic;
1. Dynamically generate a drop and create script for a table to be imported.
2. Dynamically generate the format file (needed for the insert into table, created by step 1).
3. Dynamically generrate the insert into statement for inserting into table (created in step 1).
This way you can fully dynamically do text file import for csv related files (instead of building thousands of SSIS packages to do bulk insert…..)
Some of the paramaters in the format file are now hard coded, offcourse all the variables in the format file can be put into the meta data. The definition of the XML needed is located here on MSDN;