Solved

import flat file into two tables

Posted on 2002-04-09
3
214 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
3 Comments
 
LVL 142

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how the fundamental information of how to create a table.

760 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

19 Experts available now in Live!

Get 1:1 Help Now