读取文本文件到数据库

| 1 Comment

--**************************************
-- 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

1 Comment

在你的C盘写一个txt文件,然后试试这个:

create table test(id int identity,txt varchar(8000))
insert into test(txt) exec master..xp_cmdshell 'type c:\a.txt'

--update,delete,or insert test表

exec master..xp_cmdshell 'bcp
      "select txt from 数据库名..test order by id"
       queryout c:\a.txt  -c  -S 服务器名  -U sa -P 密码'

About this Entry

This page contains a single entry by Sky published on August 24, 2005 11:23 AM.

动态获取当前屏幕中光标所在位置的颜色 was the previous entry in this blog.

Java陷阱一箩筐: 第一日答案与解析 is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.