Hey,
I've two set of codes which might help you to resolve the issue.
First one is to read the Data from a Text File.
EXEC sp_addlinkedserver txtsrv, N'Jet 4.0',
N'Microsoft.Jet.OLEDB.4.0',
N'c:\data\',
NULL,
N'Text';
GO
EXEC sp_addlinkedsrvlogin txtsrv, FALSE, sa, NULL;
GO
EXEC sp_tables_ex txtsrv;
GO
SELECT *
FROM txtsrv...[distqry#txt];
Now you'll get the data in a table and that you can copy to a temp table. Process it however you wanted and create the text
and Here is another set of code to write text Data into a file and create a file from SQL Server.
DECLARE @Text AS VARCHAR(100)
DECLARE @Cmd AS VARCHAR(100)
SET @Text = 'Hello world'
SET @Cmd ='echo ' + @Text + ' > e:\AppTextFile.txt'
print @cmd
EXECUTE Master.dbo.xp_CmdShell @Cmd