1

I'm looking for a solution to count the columns (Not rows) from a Flat File with a Script Task and store them to a variable.

Thanks in advance

btw, SQL Server 2008R2

Denis1893
  • 65
  • 2
  • 10
  • What type of file is it? Comma delimited, Tab Delimited, and how are the rows delimited? – Zane Jul 17 '14 at 15:26
  • Look [here for some help](http://stackoverflow.com/questions/6329853/ssis-column-count-from-a-flat-file). – Zane Jul 17 '14 at 16:04
  • Its a simple .txt File with Tab delimited and LF for rows. I found your link before but I didnt't get it :) – Denis1893 Jul 17 '14 at 21:11

1 Answers1

0

I think you can achieve this with Execute SQL Task using OpenRowSet or OpenDataSource

select *
  into #temp
  from OpenRowSet(...)
 where 1=2

select count(*) 
  from tempdb.sys.columns 
 where object_id = object_id('tempdb..#temp')

drop table #temp

OpenRowSet will like

select * into #temp from openrowset('MSDASQL', 'driver={Microsoft Text Driver (*.txt; *.csv)}; defaultdir=C:\MyFolder;','select * from file.csv')

and then use sp_OA procedures to launching Scripting.FileSystemObject. Open the file, and read the first line. # columns = # of commas + 1.

opendatasource() function against text file:

  select * 
from OpenDataSource( 'Microsoft.Jet.OLEDB.4.0',
'Data Source="c:\files";Extended properties="Text;hdr=no"')...clients#txt

sp_OA method:

declare @FileName varchar(100)
set @FileName = '<Put your filename here>'

declare @oFSO int,
        @oTSO int,
        @line varchar(500)

-- get the first line from the file
execute sp_OACreate 'Scripting.FileSystemObject', @oFSO OUTPUT
execute sp_OAMethod @oFSO, 'OpenTextFile', @oTSO OUTPUT, @FileName
execute sp_OAMethod @oTSO, 'ReadLine', @line OUTPUT
execute sp_OAMethod @oTSO, 'Close'
execute sp_OADestroy @oTSO
execute sp_OADestroy @oFSO

;with CTE AS
(
select Col = substring(@line, Number, 1)
  from dbo.Numbers
 where Number <= len(@line)
)
select ColumnCount = count(*) + 1, @line
  from CTE
 where Col = ','

More about OPENDATASOURCE - ad-hoc-querying-text-files

Vikramsinh Shinde
  • 2,742
  • 2
  • 23
  • 29
  • I think you may have over complicated this problem. – Zane Jul 17 '14 at 15:24
  • I hope you are right, Zane. Actually my complete plan is to take the script task and put it in an Foreach Loop Container. Because I get different txt files with different number of columns. So my plan was to check each file and write the number into the variable. Then I can manage every file with an expression (e.g variable = 10) to its own table. And u have to know, that there are only five or six possibilities of numbers for each file. So two of six files are having 15 columns. Another has 17 etc. --- I hope you can understand what I'm planning :D – Denis1893 Jul 17 '14 at 21:41
  • I have to add, that I only need the first 10 columns, from each file. But sometimes, for the same destination table, I'm getting various FlatFiles with different number of columns. An thats the problem. – Denis1893 Jul 18 '14 at 07:18
  • Another possible soultion could be to load all necessary columns in every .txt file (in an Foreach Loop Container) no matter if there are more columns after the necessary ones. Short explanation: I only need the first X columns, all after that is not important. But sometimes one file has the X necessary columns + 8 unnecessary columns, another has only the necessary columns and again another file has the X necessary columns and 16 unnessary columns. – Denis1893 Jul 18 '14 at 08:05