Technical Article

Create a bcp format file for a specified table

,

This script will create a standard format file for a specified table for use with bcp or Bulk Insert.

--Save as non - unicode
set nocount on
declare @tblname sysname
set @tblname = 'spt_values'
print '-- Delete this line and the 2 header lines below. Then save this output as non-unicode'
select '8.0' + char(13) + char(10) + char(13) + char(10) +
       (select cast(max(b.colid) as varchar) 
          from sysobjects a join syscolumns b 
            on a.id = b.id and a.name = @tblname)
        + char(13) + char(10) + char(13) + char(10)
Select cast(y.colid as varchar) + space(8 - len(y.colid)) + "SQLCHAR" + space(7) + "0" + space(7) +
       cast(y.length as varchar) + space(9 - len(y.length)) + 
       case when y.colid = (select max(b.colid) 
                              from sysobjects a join syscolumns b 
                                on a.id = b.id 
                               and a.name = @tblname)
            then '"\r\n"' + space(4)
            else '""' + space(8)
       end + 
       cast(y.colid as varchar) + space(6 - len(y.colid)) + convert(varchar(48),y.name) +
       space(50 - len(y.name)) + 'SQL_Latin1_General_CP1_CI_AS'
 from sysobjects x join syscolumns y
   on x.id = y.id 
  and x.name = @tblname

Rate

3 (1)

You rated this post out of 5. Change rating

Share

Share

Rate

3 (1)

You rated this post out of 5. Change rating