Solved

SQL and SSIS help please

Posted on 2011-02-11
8
230 Views
Last Modified: 2012-05-11
Hi there,

Can anyone help with this please. I am trying to create an ssis package to handle the following. A csv file needs to be inserted into a table. An example csv looks like this.

B, 1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 2, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 3, 30, 31, 32
CV, 1, 10, 11, 12, 13, 2, 21, 22, 23, 3, 31, 32, 33, 34, 35, 36, 37, 4, 47, 5, 6, 7, 8, 9

The number of columns can vary.

My ultimate aim is to populate a table with one column, so the above data would look like this.

B1
B10
B11
B12
B13
..
CV1
CV10
CV11
...

etc

Can someone help with this please?


0
Comment
Question by:Fulgent
  • 4
  • 4
8 Comments
 
LVL 11

Expert Comment

by:rajvja
ID: 34869329
Hi

You can use script task and write some dotnet code as below


            connectionString = "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" + importFolder + ";"
            conn = New Odbc.OdbcConnection(connectionString)

            da = New System.Data.Odbc.OdbcDataAdapter("select * from [" + fileName + "]", conn)
            da.Fill(dt)

you can query the dataset table and get your value and save to a variable
Then run the next task on this value
0
 

Author Comment

by:Fulgent
ID: 34869475
Thanks for the reply, couple of questions ..

Does the destination table have to exist before ssis can write to it, or can it build the table dynamically (the csv files will have a varying number of columns)

Say I have a table like this

Pcode      Val1      Val2
A      1      2
B      1      NULL
C      9      14

What SQL would I need to create a single column table like this?

A1
A2
B1
C9
C14

0
 
LVL 11

Expert Comment

by:rajvja
ID: 34869556
Hi

You can do both ways.
If the table already exists, you can map the columns returned by script task.
Or, you can create table with in the script task and insert into the table from the values returned from csv file.
0
 

Author Comment

by:Fulgent
ID: 34869593
Can you help with the SQL?
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 11

Accepted Solution

by:
rajvja earned 500 total points
ID: 34869663
Hi

  Find the attached code. I have done this in my prev project. This will give you an idea.

 ProcessCSV-File.txt
0
 

Author Comment

by:Fulgent
ID: 34869688
I can see how that uses bulkcopy to get the data into a table using ssis.

What I need is the sql to create my 1 column table?
0
 
LVL 11

Expert Comment

by:rajvja
ID: 34869750
Hi,

You can create the table with single column(create table mytable (col varchar(50))
Execute that script. Once you get the csv file into columns. Loop through each column and insert into the table.
0
 

Author Comment

by:Fulgent
ID: 34869858

I would prefer to use sql to make my 1 column table, can anyone help?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how the fundamental information of how to create a table.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now