Solved

import flat file into two tables

Posted on 2002-04-09
3
220 Views
Last Modified: 2006-11-17
trying to load a text files into 2 mssql tables. the text file contains rows of record where if it starts with H (header) will go to tab1 & if it starts with numeric will go to tab2.

example of the text file are like this
http://www.geocities.com/alexlow2020/sample.txt

and i have to import into below table
http://www.geocities.com/alexlow2020/table.jpg

the text file have fix char location & i have to extract it out to put into specific col.

anyone have any ideas how to do this or done it b4 ? thanks

0
Comment
Question by:gotreply
[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
3 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 6930551
For ANY kind of import, i do import to a staging table first, which has only varchar field(s). Then, with "simply" SQL, i can validate the data, and move it around to other tables, eventually other staging tables.

You will have to write some long SQL, specifiying all the columns, splitting up the data, but you can be assured that this is the most easy-to-support and flexible solution i found until now.

CHeers
0
 
LVL 1

Accepted Solution

by:
gotaquestion earned 200 total points
ID: 6937352
you should import the file into one staging table like angelll suggested.  and you should add a field to this staging table to be used for a flag as to which table the row is going to eventually insert to.  use the Data Transformation Service and
and in the ActiveX script Transformation properties
determine the flag

'**********************************************************************
'  Visual Basic Transformation Script
'************************************************************************

'  Copy each source column to the destination column
Function Main()
     DTSDestination("flag") = getFlag()
     DTSDestination("f2") = DTSSource("Col002")     DTSDestination("f3") = DTSSource("Col003")
etc..

     Main = DTSTransformStat_OK
     
End Function
Function getflag()

     ''some sort of test
     if isnumeric(DTSSource("headerfield")) then
        getflag = "table1"
        else
        getflag = "table2"
     end if

End Function


after the dts is done you can run an insert into your main table based on the flag field

you could so set up multiple destinations in a DTS object but that is more complicated.
0
 

Author Comment

by:gotreply
ID: 7303807
thks for the help, have solve it after various try, ur comment did give me some idea to furthur my experiment.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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.

627 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