September 17, 2007 at 7:15 am
Hi,
I’m working on the fallowing case:
I have to import data form table to text file in SQL server 2000 with stored procedure. Well.. that must be no problem. But my question is: is it impossible to add automatically smart headers such as (date, time, Row count…enz) to the end of the text file how I have to import to during the execution of the stored procedure.
thx.
Cees
September 17, 2007 at 8:12 am
Hello,
I suppose that from the viewpoint of SQL Server this is export, not import... and I'm a bit confused - what do you mean by smart headers, especially if they should be at the end of file? Header is mostly at the beginning, on top of page. Could you show us some short example, a few rows of data in the database (or result of some SQL) and how it should look in the text file?
September 17, 2007 at 9:42 am
look at this script from the contributions, which opens a file and appends to the end of it:
you could BCP to a file, and then use the script to append to teh bottom of it.
http://www.sqlservercentral.com/scripts/viewscript.asp?scriptid=1449
you could also use this to write a header, and then use bcp to append to teh end of that.
Lowell
September 18, 2007 at 3:37 am
Vladan,
you are right about the export and headers things. well what i want to have is:
The table i have look like this:
Column1 Column2
1 Hi
2 Hallo
The text file should look like this:
Date: 09/18/2007
Time: 13:34
Total rows: 2
1 Hi
2 Hallo
September 19, 2007 at 12:57 am
Hi,
It looks like my question is too difficult or not so clear, so I will like repeat my question.
I want to export data from table in SQL server 2000 to text file. During the expert process I need to add some info about the exporting process on the header of the text file, as date, time and total row. It should look like this:
HEADER1:53910072007000000000086456
1;rr;dat;day
2;tt;that;day
I hope my question is a little clearer than before..
Thx,
Cees
September 19, 2007 at 6:55 am
Yes... easily done... but you need to learn what ">>" means from the CMD prompt (or xp_CmdShell). Lookup "Using command redirection operators" in Windows help (under the Windows [Start] button...)
--Jeff Moden
Change is inevitable... Change for the better is not.
September 19, 2007 at 7:03 am
bcp doesn't allow you to append files, but the COPY command certainly is able to do it.
here's a complete example: it uses the function i suggested above.
psuedocode: bcp, make header, combine two files together.
--table to capture xp_cmdshell output
CREATE TABLE #results(resultstext varchar(1000) )
declare @sql varchar(4000),
@rowcount int
--sample query: you would do the same to your existing bcp
--note i erased the password for the -P flag...make sure to fix
set @sql = 'bcp "SELECT TOP 5 * FROM SYSOBJECTS" queryout "c:\body.txt" -c -U"sa" -P""'
--export via bcp
insert into #results
EXEC master..xp_cmdshell @sql
SET @rowcount=@@rowcount --used only as example that that is NOT the rows you wanted to count
print @rowcount -- this is the rows from xp_cmdshells work, NOT the bcp!!!!
--you have to get it from the output of xp_cmdshell
--select * from #results where resultstext like '%rows copied.'
select @rowcount = convert(int,substring(resultstext,1,charindex(' ',resultstext) ) ) from #results where resultstext like '%rows copied.'
--header preamble
SET @sql = 'HEADER1:53910072007000000000086456'
SELECT DBO.Ufn_WriteToFile('c:\header.txt',@sql)
--header date
SET @sql = 'Date: ' + convert(varchar,getdate(),101)
SELECT DBO.Ufn_WriteToFile('c:\header.txt',@sql )
--header time
SET @sql ='Time: ' + convert(varchar,getdate(),108)
SELECT DBO.Ufn_WriteToFile('c:\header.txt',@sql )
--header rowcount
SET @sql ='Total rows: ' + convert(varchar,@rowcount)
SELECT DBO.Ufn_WriteToFile('c:\header.txt',@sql)
--combine the header and query together into a final file.
set @sql ='copy c:\header.txt + c:\body.txt c:\results.txt'
EXEC master..xp_cmdshell @sql
drop table #results
Lowell
September 19, 2007 at 7:25 pm
Hi, It looks like my question is too difficult or not so clear, so I will like repeat my question. I want to export data from table in SQL server 2000 to text file. During the expert process I need to add some info about the exporting process on the header of the text file, as date, time and total row. It should look like this:
HEADER1:53910072007000000000086456 1;rr;dat;day 2;tt;that;day
I hope my question is a little clearer than before.. |
Considering that you just changed the header requirements in a major fashion, I'm not so sure that you're clear on what you need...
If we consider ...
HEADER1:53910072007000000000086456
... to be the actual header that you want, would you at least describe the layout of the header by character postition so that we may give you an example that "is a little clearer than before.."? The date of 10072007 is pretty obvious... but what's all the other stuff?
--Jeff Moden
Change is inevitable... Change for the better is not.
September 20, 2007 at 1:15 am
Lowell,
Thank you very much.. It works.. You don't have any idea how long I was busy with this script..
thx again..
Cees
September 20, 2007 at 7:06 am
Lowell does do a great job, but lemme ask you Cees... have you calculated the header or are you going to manually change it in the code?
--Jeff Moden
Change is inevitable... Change for the better is not.
September 20, 2007 at 7:56 am
awesome! glad it worked out for you.
It was a neat challenge to try and figure that one out. It's really just grabbing building blocks of code I used before, and putting them in the right order.
Lowell
September 21, 2007 at 3:07 am
Dear Jeff,
I have to calculated, 53910072007 parts is date and time (hmiddmmyyyy) and 000000000086456 parts is total row. With Lowell scripts and after a little changing I can make the following header HEADER1:5391007200786456. Aim searching now to a way to add the 0000000000 part to the header too. 000000000086456 parts moest be always 15 character.
thx.
Cees
September 21, 2007 at 4:51 am
to do what yoiu are asking, you need to take the RIGHT() 15 chars of 15 '0' + the convert of your value.
for example
SELECT RIGHT('000000000000000' + CONVERT(VARCHAR,GETDATE(),112) ,15)
look at this thread for exactly that example:
http://www.sqlservercentral.com/forums/shwmessage.aspx?forumid=9&messageid=400056#bm400103
Lowell
September 21, 2007 at 5:32 am
Ok, Cees... I was just making sure that you had everything that you needed. And Lowell's snippet above will take care of the zeros...
--Jeff Moden
Change is inevitable... Change for the better is not.
September 21, 2007 at 7:34 am
Lowell,
I thing my question was not so clear, as i say before the 000000000086456 parts is always 15 character, that mean when i get a higher row number for example 765486456 then I have automatically to fill the rest of the character zeros so it must look like this 000000765486456.
anyway thx for you above tip..
thx..again..
Cees
Viewing 15 posts - 1 through 15 (of 17 total)
You must be logged in to reply to this topic. Login to reply