Important alert: (current site time 7/15/2013 8:05:53 PM EDT)
 

VB icon

Read a text file into SQL SERVER

Email
Submitted on: 11/5/2001 3:24:44 PM
By: Eli Leiba  
Level: Advanced
User Rating: By 7 Users
Compatibility: SQL Server 7.0
Views: 70502
author picture
(About the author)
 
     Using BULK INSERT TO READ A TEXT FILE into SQL SERVER and ELIMINATING THE USE of another text editor.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
--**************************************
-- Name: Read a text file into SQL SERVER
-- Description:Using BULK INSERT TO READ A TEXT FILE into SQL SERVER and ELIMINATING THE USE of another text editor.
-- By: Eli Leiba
--
-- Inputs:@filename sysname (filename includes path)
--
-- Returns:a recordset of the file lines in QUERY analyzer
--
-- Assumes:Usage :
 exec sp_readTextFile 'c:\autoexec.bat'
--
--This code is copyrighted and has-- limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=359&lngWId=5--for details.--**************************************

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 


Other 20 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

1/26/2002 1:26:40 PMSelva

This is very very useful one.


(If this comment was disrespectful, please report it.)

 
4/26/2002 2:46:41 AMTheNeophyte

thanks!
(If this comment was disrespectful, please report it.)

 
4/29/2002 3:07:15 AMTheNeophyte

is there a reverse thing of this? i mean, from the database i will write the fields of all the tables into a textfile using a sql statement? if so, may i know how? thanks
(If this comment was disrespectful, please report it.)

 
3/26/2003 10:16:17 AM

Do you no of a similar command in Oracle to popualte a table from a file, Thanks
(If this comment was disrespectful, please report it.)

 
10/25/2003 3:13:36 PMCarl Mercier

Nice code!

Can you tell me if this would work with a binary file (assuming that I convert the field type to image, of course)

Thanks!

(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.