Solved

creating a qurey to import mutiple flat file content into specific existing tables

Posted on 2011-09-02
2
270 Views
Last Modified: 2012-05-12
hi,
I am using sql server 2k5 to import 264 flat file contents into each matching table.  I have a long way to go.  I perform the following steps below.  Please share an examples of a query that uses the criteria below to import data from multiple flat files from a folder on  a pc into its related database tables.  Thanks

Import Steps I perform
Source: Flat File
Locale: English (United States)
Code page: 1252 (Ansi - Latin 1)
Format: Delimited
Text Qualifier: "
Header row delimiter: {CR}{LF}
Header rows to skip: 0
Column names in the first data row: yes
Edit Mappings-->Column Mappings-->Delete Rows in destination table

Choose a Destination
Destination: SQL Native Client
Server Name: dbsrv01
Use Windows Authentication
Database: dbTrains
0
Comment
Question by:cesemj
2 Comments
 
LVL 39

Accepted Solution

by:
lcohan earned 500 total points
ID: 36475395
been there done that....think a folder is receiving "flat" files and you need to import them into SQL tables (for simplicity) with same name.

I used:

EXEC xp_cmdshell N'dir G:\FTP_DOWNLOADS\CSP_file.txt';

cmd shell to get their names in a temp table than I used that table to run a SP like below wich will import your flat file into a SQL table. With little bit of work you can adjust it and creat a ne table for each new impot file or put it into an existing table:


--Usage : exec sp_readTextFile 'G:\FTP_DOWNLOADS\CSP_file.txt'
Create proc sp_readTextFile @filename sysname
as

    begin
    set nocount on
    Create table #tempfile (line varchar(8000))
    exec ('bulk insert #tempfile from "' + @filename + '"')
    select * from #tempfile
    drop table #tempfile
End
go




0
 

Author Closing Comment

by:cesemj
ID: 36498028
Thanks
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This tutorial will walk an individual through locating and launching the BEUtility application to properly change the service account username and\or password in situation where it may be necessary or where the password has been inadvertently change…
This tutorial will walk an individual through the steps necessary to install and configure the Windows Server Backup Utility. Directly connect an external storage device such as a USB drive, or CD\DVD burner: If the device is a USB drive, ensure i…

705 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