RE: Bulk Copy / BCP problem....please advise
From: FWalton (anonymous_at_discussions.microsoft.com)
Date: 09/24/04
- Next message: Curtis Justus: "Rounding Best Practice"
- Previous message: Mike John: "Re: Stored Prcedure Security"
- In reply to: Scott: "RE: Bulk Copy / BCP problem....please advise"
- Next in thread: Steve Kass: "Re: Bulk Copy / BCP problem....please advise"
- Messages sorted by: [ date ] [ thread ]
Date: Fri, 24 Sep 2004 14:51:23 -0700
Yea, Bulk Copy and BCP you have to specify a static
destination and pretty much an exact match unless you use
a format file. But if it changes you're hosed.
Here's something I've done before. Use the Text ODBC
driver with either OpenRowset or OpenQuery and load into
a temp table first. OpenQuery you have to build a linked
server. OpenRowset you can specify the DSNless ODBC Text
Driver connect string right in your TSQL, but
theoretically is a little slower as the server has to
parse/confirm the connection string each time rather than
a DSN/Linked Server. Look at the help for both and
you'll see what I'm talking about.
Anyway, in the T-SQL procedure you setup to import the
file...
Select *
Into #Temp
>From OpenRowset(<OCBC; Connect String>,
'Select * From C:\TextFile.txt')
The text file will be loaded into an automatically
created temp table regardless of the varying number of
columns. (I think they all will default to text fields
usually when you do this.) From there you can add
additional steps to pull out the columns you need and do
whatever else you need.
One thing that may bite you is the field/row seperators
in your file. I believe you can specify tab, comma,
etc... in the text odbc connect string. Play around and
see.
Even if the file has seperators that text ODBC won't
accept you still aren't out of luck. Import the file
into a single field table and run an update replacing
the "bad" sepeators with comma or tab, then bcp it back
out to a file, then reload the new file with the sql
above and text odbc. Ugly and slow, but you can
massage/load the file all within T-SQL without any
external SQL steps or nasty T-SQL parsing.
I've even done this in conjunction with an automated FTP
transfer and load from T-SQL. I made a one field table
with automated FTP commands and BCPed it out to a file,
then ran xp_cmdshell and called FTP with the command line
options to use the BDP'd FTP config file for the steps.
It copied the file then loaded it. Probably the nastest
T-SQL I've ever thrown together but it works great.
Good Luck!
- Next message: Curtis Justus: "Rounding Best Practice"
- Previous message: Mike John: "Re: Stored Prcedure Security"
- In reply to: Scott: "RE: Bulk Copy / BCP problem....please advise"
- Next in thread: Steve Kass: "Re: Bulk Copy / BCP problem....please advise"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|