Help with echo in xp_cmdshell

  • Declare @Footer varchar(1000)

    Set @Footer = 'echo RECORD COUNT: >> D:\Work\Test.txt'

    exec xp_cmdshell @Footer

    This works fine.

    But this command also inserts a default <CRLF> at the end of 'RECORD COUNT:' when written to the file.

    How can I avoid this?

    I tried using the \c, \b..but instead of doing anything about it, it just goes ahead and also prints \c or \b

    I have even tried preceding the \c with a ^

    HELp!!!

  • namrata.dhanawade-1143388 (2/23/2011)


    Declare @Footer varchar(1000)

    Set @Footer = 'echo RECORD COUNT: >> D:\Work\Test.txt'

    exec xp_cmdshell @Footer

    This works fine.

    But this command also inserts a default <CRLF> at the end of 'RECORD COUNT:' when written to the file.

    How can I avoid this?

    The <CRLF> is added by echo DOS command which you can't turn it off as of my understanding.

    I tried using the \c, \b..but instead of doing anything about it, it just goes ahead and also prints \c or \b

    I have even tried preceding the \c with a ^

    echo will print whatever text you provide after it. It doesn't have a option \c, \b etc.

  • It looks like you are trying to create some sort of structured output data file from SQL Server using xp_cmdshell. This is usually a bad idea, for all sorts of reasons. Can you not find a better way to do this, perhaps via an external application or using a SQLCLR procedure or function?

  • Requirement:

    There are 100 tables each with different number and type of columns and obviously different rows.

    And then there is a condition to check if some 5 columns exists, if they do, then filter them with a where clause, else just ignore the column altogether.

    they want the resulting data into separate .txt files.

    I had wanted to do this using SSIS, however, that would need 100 packages since all the tables have different structure.

    If you can suggest a work around using SSIS, I'd be greatful.

  • namrata.dhanawade-1143388 (2/23/2011)


    Requirement:

    There are 100 tables each with different number and type of columns and obviously different rows.

    And then there is a condition to check if some 5 columns exists, if they do, then filter them with a where clause, else just ignore the column altogether.

    they want the resulting data into separate .txt files.

    I had wanted to do this using SSIS, however, that would need 100 packages since all the tables have different structure.

    If you can suggest a work around using SSIS, I'd be greatful.

    Sorry, I may be misreading but it is unclear to me, will the column list in the result set be the same when selecting from all 100 tables?

    Something like:

    SELECT SpecialColumn1of5,

    SpecialColumn2of5,

    SpecialColumn3of5,

    SpecialColumn4of5,

    SpecialColumn5of5

    FROM dbo.TableImCurrentlyWorkingOn

    WHERE SpecialColumn1of5 > 'some criteria''

    Or will you have different column lists and where-clause criteria for each of the 100 tables?

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

Viewing 5 posts - 1 through 4 (of 4 total)

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