Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SQL and SSIS help please

Posted on 2011-02-11
8
Medium Priority
?
236 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Fulgent
ID: 34869593
Can you help with the SQL?
0
 
LVL 11

Accepted Solution

by:
rajvja earned 2000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

636 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