20 March 2008

How to read a text file by using stored procedure in Microsoft SQL Server?

SET NOCOUNT ON

/* Create a temporary table to store the data in the text file. Maximum character is 8000. */
CREATE TABLE #tempfile (line varchar(8000))

/* Insert data from the text file to the temporary table */
EXEC ('bulk INSERT #tempfile FROM "' + 'C:\Sample.txt' + '"')

/* Return the data in the text file. */
SELECT * FROM #tempfile

/* Drop the temporary table. */
DROP TABLE #tempfile

No comments: