|
I've "text file" stored in a column of a table in SQL Server database. How do I retrieve the text file from the database.
(comments are locked)
|
|
The following BCP utility worked for the issue I posted earlier. The only issue pending is getting the timestamp from BillDt column. For formatting purpose we're inserting @@@ on all header records but what we noticed is that the BillDt (timestamp) is not displayed properly (see below the results from bcp utility). So, How do I get the BillDt in the correct format? BCP Utility: C:>bcp "SELECT TOP 5 '@@@', [BillDt], [FileName], [FileExt], [BillImage] FROM [Eclips].[dbo].[tblBill] WHERE BillType in ('ITBP2007', 'CURRENT_MONTH_DEMAND' )" queryout C:TempeClipseClipsdata.dat -N -S SQLSERVER01TEST -T Results from BCP utility: @ @ @ ?ž H 9 9 9 9 - 9 9 9 9 - 9 9 _ 2 0 1 0 - 1 2 - 0 1 _ I T B P 2 0 0 7 . h t m H T M öu
(comments are locked)
|
|
If I understand you right, you have a table in which there is a column that is either TEXT, NTEXT, VARCHAR(MAX) or NVARCHAR(MAX). You want to save the value in every row to a separate file. The obvious way of doing this is to use a PowerShell script, especially if you wish to save on the 'client' rather than the Server. If you need to extract the text as files on the server, as part of an archiving perhaps, or to do a file-based process, there are a variety of different ways. I'd recommend using PowerShell, again, but it depends on the circumstances, and the systems that you are most familiar with. If you need to do it in TSQL via BCP, then that's possible. If each row represents a file, then I think you'll have to get each 'text file' value from the table and then execute this procedure to save it to disk, supplying the file path for each row. Here is a general procedure to write out a VARCHAR(MAX) to disk as a file. (taken from my article The TSQL of text files . I suspect that there is a much cleaner way of doing this in your case since the text file is already a row in a permanent table, and my routine is for a variable; but I haven't the time to write it (and test it!). ----------------------------------------------------------------- IF OBJECT_ID (N'dbo.spSaveTextToFile') IS NOT NULL DROP PROCEDURE dbo.spSaveTextToFile GO CREATE PROCEDURE spSaveTextToFile @TheString VARCHAR(MAX), @Filename VARCHAR(255), @Unicode INT=0 AS SET NOCOUNT ON DECLARE @MySpecialTempTable VARCHAR(255) DECLARE @Command NVARCHAR(4000) DECLARE @RESULT INT +1 - excellent attempt suggestion, let's hope it's close to what the question was asking for...
Sep 23 '11 at 02:10 AM
Fatherjack ♦♦
Sorry I didn't post my question very clearly. I'm new to SQL Server and working on a task to extract files that are stored in SQL Server table and FTP all the files to Vendor. Table SELECT [ID],[AccountID],[BillDt],[BillType],[FileName],[FileExt],[FileCreateDt],[FileModifiedDt],[FileSize],[EntryDt],[EnteredBy],[ModifiedDt],[ModifiedBy],[BillImage] FROM [Eclips].[dbo].[tblBill] Last column 'BillImage' datatype is declared as (Image,null). Depending on the value in column FileExt (file extension) data in 'BillImage' can be a 'txt', 'afp' or 'htm' file. I first want to extract all the txt and html files from 'BillImage' Column and store them to a folder. When I extract it I'm getting the files in Binary format. Is there any way of retrieving and exporting the image files in their original format (text and htm) so that I don't have to writing a conversion program?
Sep 26 '11 at 06:12 AM
sritej
You could use cast of your image column to varchar(max), but there is a small twist. The image data type (which by the way will be deprecated in future versions of SQL Server) cannot be directly converted to varchar. It first needs to be converted to varbinary(max). For example, if you want your BillImage to show the values in the varchar format then you can use this: SELECT
[ID],[AccountID],[BillDt],[BillType],[FileName],
[FileExt],[FileCreateDt],[FileModifiedDt],[FileSize],
[EntryDt],[EnteredBy],[ModifiedDt],[ModifiedBy],
cast(cast([BillImage] as
varbinary(max)) as varchar(max)) [BillImage]
FROM [Eclips].[dbo].[tblBill];
Sep 26 '11 at 08:31 AM
Oleg
There is a great article on Simple-talk by 'Pop Rivett', called 'Pop Rivett and the FTP Directory' http://www.simple-talk.com/sql/t-sql-programming/pop-rivett-and-the-ftp-directory/ but that tells you how to import into SQL Server, rather than export. However the two processes are remarkably similar. With Oleg's suggestion, my stored procedure, and Pop Rivett's code you should be home and dry. Let us know if you need further help with this.
Sep 26 '11 at 09:10 AM
Phil Factor
@Oleg. That's great advice. I had no idea it was that convoluted!
Sep 26 '11 at 09:11 AM
Phil Factor
(comments are locked)
|


@sritej What is the data type of the column storing the text file? If it is varchar(max) or nvarchar(max) and the file is a plain text file then you can see the whole text when you issue a select statement including the column in question. If the file is not a plain text file and/or the data type of the column is varbinary(max) then please elaborate on your requirement a bit. Do you want to just read the contents or you need to read the contents and store them on the disk somewhere?