Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Insert text into tables

Author  Topic 

zhshqzyc
Posting Yak Master

240 Posts

Posted - 2011-06-13 : 15:58:59
Hi,

I have a bunch of text files (saying 100 files) in a directory. I want to create tables that correspond files respectively.
Each text file only has one very long string, so in each table there is only one row/column. I don't want to copy and paste one by one.

Is it possible?

Thanks.

robvolk
Most Valuable Yak

15732 Posts

Posted - 2011-06-13 : 16:09:27
If you don't mind a DOS/command line solution, you can use the for command. Open a command window and change to the directory containing the files, then run this:

for %a in (*.txt) do sqlcmd -Sserver -ddatabase -E -Q"create table [%~na](longColumn varchar(max))"

Once you run that you can bcp the data in:

for %a in (*.txt) do bcp "database..%~na" in %a -Sserver -T -c

Change the "database" and "server" names to match yours. You can see more examples of the for command here: http://weblogs.sqlteam.com/robv/archive/2003/09/28/181.aspx
Go to Top of Page

zhshqzyc
Posting Yak Master

240 Posts

Posted - 2011-06-21 : 13:29:44
I am going to use BULK INSERT. But the result is wrong. Could you please look at my code?
I put all files path in a text file "list.txt". Then using a cursor to go through the table insert the context one by one.
USE Bible;
GO

CREATE table dbo.FileLists
(
ID INT NOT NULL IDENTITY PRIMARY KEY,
FilePath NVARCHAR(128),
)


CREATE TABLE dbo.ImportedTextFile
(
ID INT NOT NULL IDENTITY PRIMARY KEY,
FilePath NVARCHAR(128),
CONTENT NVARCHAR(MAX)
);
GO
-- Insert a sample record
INSERT INTO dbo.FileLists(FilePath) VALUES ('C:\Bible\list.txt');
GO

-- Read a sing text file based on file path and reture the text file content
CREATE PROCEDURE dbo.ReadSingleTextFile
(
@FilePath NVARCHAR(128),
@FileContent NVARCHAR(max) output
)
AS
BEGIN
SET NOCOUNT ON;
CREATE TABLE #Temp(Content nvarchar(max))
EXEC('BULK INSERT #Temp FROM "' + @FilePath + '"');
SELECT @FileContent = Content FROM #Temp
DROP TABLE #Temp
END
GO

-- Read all files into a table based on a list of file paths in aother table
SET NOCOUNT ON;

DECLARE @FilePath nvarchar(128);
DECLARE @Content nvarchar(max);

DECLARE FilePath_Cursor CURSOR FOR SELECT FilePath FROM dbo.FileLists;

OPEN FilePath_Cursor;

FETCH NEXT FROM FilePath_Cursor INTO @FilePath;

WHILE @@FETCH_STATUS = 0
BEGIN
EXEC dbo.ReadSingleTextFile @FilePath, @FileContent = @Content output;
INSERT INTO dbo.ImportedTextFile(FilePath, Content) VALUES(@FilePath,@Content)
FETCH NEXT FROM FilePath_Cursor INTO @FilePath;
END
CLOSE FilePath_Cursor;
DEALLOCATE FilePath_Cursor

-- Test the result
SELECT * from dbo.ImportedTextFile


The context in list.txt looks like
C:\Bible\1chron.txt
C:\Bible\1corinth.txt
C:\Bible\1john.txt
C:\Bible\1kings.txt
C:\Bible\1peter.txt
C:\Bible\1samuel.txt
C:\Bible\1thess.txt

Thank you very much!
Go to Top of Page
   

- Advertisement -