Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin.
Search for scripts directly from SSMS, and instantly access any saved scripts in your
SSC briefcase from the favorites tab.
Download now (direct download link)
Thank this author by sharing:
By Joey Moelands,
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;
Dynamically Creating Indexes
Create Variable Dynamically in Stored Procedure
insert data using dynamic open row set
DYNAMIC SELECT & INSERT QUERY HELP
Issue on Insert datas in Dynamic Query