in reply to loading a txt file into SQL Server
I don't believe this is the proper place to get this information, but I'm not sure I know the full extent of the problem. Is this import you are trying to do something you are trying to make an automated process or is it something you will do infrequently and just need a tool for the import? If you just need a tool for the import, SQL Server Enterprise Manager provides everything you need.
In that case open Enterprise Manager. Expand the "Microsoft SQL Server Nodes" branch and keep expanding until you see what looks like a list of folders. Then expand the "Databases" folder. Right-click on the database into which you would like to import the data (or create a new database first) and select "All Tasks | Import Data..."
At this point, you'll be prompted with a wizard that will walk you through the import proceess. Click "Next>>". On the next screen, you'll see a drop down list labeled "Data Sources". In that drop down, select "Text File" which is the second from the bottom in the list on my system. From there you should be able to figure out the rest.
Now, of course, if you want to do this regularly with a script to automate something, you will need to install perl on Windows (I use ActiveState). Then from a command line run ppm. This is similar to the CPAN shell which allows you to install modules on the fly. You can, at the prompt, type: install DBI. Once that has finished, type: install DBD-ODBC. Once these two modules are installed, you should be able to connect to a DSN through ODBC.
The perl code to do what you want would look something like:
#!C:/perl/bin/perl use strict; use DBI; my $filename = shift || die "Please provide a filename.\n"; my $dbh = DBI->connect( "dbi:ODBC:DSNNAME", "", "", {RaiseError => 1, PrintError => 1, AutoCommit => 1} ) or die "Unable to connect: " . $DBI::errstr . "\n"; # Open your file open( INFILE, $filename ) or die "Couldn't open file for reading.$!\n" +; while( $line = <INFILE> ) { my @cols = split( "\t", $line ); # build insert string with items in @cols my $sql = "INSERT INTO..."; $dbh->do( $sql ) or die "Couldn't insert record. $!\n"; } close(INFILE); $dbh->disconnect;
This code is untested!!
This method is not ideal for large amounts of data because you are doing one insert per record which means lots of overhead. In that case I would use the method I mentioned first of using Enterprise Manager. There may even be a way to automate that.
Good luck.
-Matt
|
|---|