Error while creat the text file using SQL server 2005

  • Hi,

    I am using the below code to create a text file from SQL server 2005.

    DECLARE @vCmd VARCHAR(MAX)

    DECLARE @vCLUEDATE VARCHAR(MAX)

    DECLARE @vWorkfilename VARCHAR(MAX)

    SET @vCLUEDATE = '000000001EFHRCF01FF 20110718205227VERI2011071820110718 2'

    SET @vWorkfilename = 'F:\ClueUpload\CVGVER.20110718.0101.txt'

    Select @vCmd = 'echo ' + @vCLUEDATE + '>' + @vWorkfilename

    EXEC master..xp_cmdshell @vCmd

    But it is creating the file without data, but it should create with one line of data.

    Then i changed the @vCmd as below

    Select @vCmd = 'echo ' + @vCLUEDATE + ' > ' + @vWorkfilename

    then it is working. but it is adding one space to the end of line that is after 2 it is adding. but i don't want that space, so how can I do this.

    Please help me on this...

    Regards,

    Sarath Babu Vellampalli

    Kindest Regards,

    Sarath Vellampalli

  • - can you elaborate on what you are actually trying to accomplish ?

    - why are you using xp_cmdshell ?

    - why aren't you using sqlcmd or bcp ? (maybe even just using a sqlagent job)

    - is there a reason not to use SSIS ?

    Johan

    Learn to play, play to learn !

    Dont drive faster than your guardian angel can fly ...
    but keeping both feet on the ground wont get you anywhere :w00t:

    - How to post Performance Problems
    - How to post data/code to get the best help[/url]

    - How to prevent a sore throat after hours of presenting ppt

    press F1 for solution, press shift+F1 for urgent solution 😀

    Need a bit of Powershell? How about this

    Who am I ? Sometimes this is me but most of the time this is me

  • Hi ALZDBA,

    I can't use SSIS because of client restriction.

    by using my code it is adding one space to the end of the line, I have to elemenate that how can I do that please explain me. if there is any other way also.

    Kindest Regards,

    Sarath Vellampalli

  • To put it another way, are you sure you (or your client) chose the right tool for the job?

    If not SSIS, then why not bcp, sqlcmd or even PowerShell via a SQL Agent job?

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

  • Hi opc,

    I am using this code in SP and we need to append the data to same text file with loop.

    so please help me on this.

    Kindest Regards,

    Sarath Vellampalli

  • where will your file be located ? Does your sqlserver service account have the auth to read/write to that location ?

    maybe have a look at the replies of this thread http://www.sqlservercentral.com/Forums/Topic1143504-392-1.aspx

    Johan

    Learn to play, play to learn !

    Dont drive faster than your guardian angel can fly ...
    but keeping both feet on the ground wont get you anywhere :w00t:

    - How to post Performance Problems
    - How to post data/code to get the best help[/url]

    - How to prevent a sore throat after hours of presenting ppt

    press F1 for solution, press shift+F1 for urgent solution 😀

    Need a bit of Powershell? How about this

    Who am I ? Sometimes this is me but most of the time this is me

  • Sarath Vellampalli (7/18/2011)


    Hi opc,

    I am using this code in SP and we need to append the data to same text file with loop.

    so please help me on this.

    I understand, but using ECHO with xp_CmdShell is pretty inflexible which is why myself and others have tried to guide you in a different direction. It may be possible, but maybe not. There are tons of examples on the web on how to use xp_CmdShell. Maybe try TYPE instead of ECHO? Good luck.

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

  • Dear,

    I know this very old post but ..

    Solution is:-

    Use

    Select @vCmd = 'echo ' + @vCLUEDATE + CHAR(254) + '> ' + @vWorkfilename

    and then excute below bat file

    copy below code and make a bat file of that.

    for /f "tokens=1,* delims=]" %%A in ('"type %1|find /n /v """') do (

    set "line=%%B"

    if defined line (

    call set "line=echo.%%line:%~2=%~3%%"

    for /f "delims=" %%X in ('"echo."%%line%%""') do %%~X> %1

    ) ELSE echo.

    )

    run above batch file using xp_cmdshell as

    Select @vCmd = 'script.bat ' + @vWorkfilename + ' ' + CHAR(254)

    EXEC xp_cmdshell @vCmd

    I hope this will help.

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply