May 21, 2014 at 10:43 am
I have table1 with col1 varchar,col2 int , col3 xml , col4 bit
Can any one suggest a best way to fetch the col3 into file (.txt or .sql) into seprate file for each col3 record . I want to export this in different files for each record if possible with date and time stamp .
Its a tricky one for me .
May 21, 2014 at 10:46 am
RantaSanta (5/21/2014)
I have table1 with col1 varchar,col2 int , col3 xml , col4 bitCan any one suggest a best way to fetch the col3 into file (.txt or .sql) into seprate file for each col3 record . I want to export this in different files for each record if possible with date and time stamp .
Its a tricky one for me .
SSIS
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
May 21, 2014 at 11:45 am
There might not be any need to go to SSIS.
If you have some kind of primary key, you could use it to generate all the files with a cursor.
DECLARE @id nvarchar(9)
DECLARE @bcp nvarchar(MAX)
DECLARE Test CURSOR READ_ONLY FAST_FORWARD
FOR
SELECT idColumn
FROM Table1
OPEN Test
FETCH NEXT FROM Test INTO @id
WHILE @@FETCH_STATUS = 0
BEGIN
SET @bcp = 'bcp "select col3 FROM Table1 WHERE idColumn = ' + @id + '" queryout ' +
'"C:\Somelocation\Somename' + REPLACE( REPLACE( REPLACE( CONVERT( char(23), GETDATE(), 126), ':', ''), '-', ''), '.', '') + '.txt"'
PRINT @bcp
--EXECUTE xp_cmdshell @bcp
WAITFOR DELAY '00:00:00.004'
FETCH NEXT FROM Test INTO @id
END
CLOSE Test
DEALLOCATE Test
There might be some errors, but I you'll have to test the code and try to figure out how to correct them (that's how you'll learn more ;-)).
May 23, 2014 at 2:34 pm
Thanks Luis ,
However , I realized extracting xml column to .txt/.sql I lose the formatting even If I extract in .xml .:crazy:
For now I am going to explore the Powershell to accomplish this . Not sure where to start but will share if I am able to .
But the same code which you shared will work for any other format type with out any problem .
May 23, 2014 at 5:41 pm
RantaSanta (5/23/2014)
However , I realized extracting xml column to .txt/.sql I lose the formatting even If I extract in .xml .:crazy:
Actually, you won't. It only looks that way in the grid mode.
--Jeff Moden
Change is inevitable... Change for the better is not.
May 24, 2014 at 6:39 am
Maybe you didn't play enough with bcp. This article might help you.http://sqlsouth.wordpress.com/2014/05/23/export-xml-from-sql-server-using-bcp/
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply