Export from SQL Server to Fixed Width format

  1. First thing is to garner what the layout of the FWF file should be? Hopefully this is available to you. In this example, purely illustrative, I am going to export the SQL Server System view that provides the list of tables in a database. This view has 4 columns so I will need 4 fixed widths in the file. They are all text and again for this example I am going to make them character length 20. Based on this I need to create a Schema.ini file first.
  2. Create a Schema.ini file to represent the layout of the final FWF file. Below is what is required in this example. Follow this format.
Schema.ini
DROP TABLE IF EXISTS [TEMPDB].[DBO].[FWF_FORMAT];SELECT [TABLE_CATALOG] = COALESCE(CAST(TABLE_CATALOG AS CHAR(20)), SPACE(20)),[TABLE_SCHEMA] = COALESCE(CAST(TABLE_SCHEMA AS CHAR(20)), SPACE(20)),[TABLE_NAME] = COALESCE(CAST(TABLE_NAME AS CHAR(20)), SPACE(20)),[TABLE_TYPE] = COALESCE(CAST(TABLE_TYPE AS CHAR(20)), SPACE(20))INTO [TEMPDB].[DBO].[FWF_FORMAT]FROM [TEMPDB].[INFORMATION_SCHEMA].[TABLES];
Connect to SQL Server and export to FWF file

References:

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Joe Hunter

Joe Hunter

Digital Health Software Engineer