exec xp_cmdshell

  • I am executing an .exe file from a stored procedure. The exe is returning some values. How to store those values in a table.

    use [tempdb]

    go

    alter procedure FirstTest

    as

    begin

    DECLARE @result as int

    exec @result = xp_cmdshell 'D:\temp\Debug\ConsoleAppModule'

    print @result

    end

    The ConsoleAppModule prints 4 values in the console. They are

    value1

    value2

    value3

    value4

    I want to catch and store these values in a table say Table1

    Column1    Column2    Column3    Column4

       Value1      Value2        Value3       Value4

    Can anyone say what needs to be modified in the stored procedure to achieve this.

  • Hi,

    Ensure your out put should in row format.

    create MYTABLE

    (

    slno int identity(1,1),

    Result varchar(20)

    )

    insert into MYTABLE

    select @result

    select max(case when slno = 1 then Result else '' end) COL1,

    max(case when slno = 2 then Result else '' end) COL2,

    max(case when slno = 3 then Result else '' end) COL3,

    max(case when slno = 4 then Result else '' end) COL4

    from MYTABLE

    This can achieve by lot of ways (row to column)

    You also see this article to get more easy ways.

    http://www.simple-talk.com/sql/t-sql-programming/concatenating-row-values-in-transact-sql/%5B/url%5D

  • You can do something like this to get the values into a table:

    CREATE TABLE #temp (VALUE VARCHAR(100))

    INSERT INTO #temp

    (

    VALUE

    )

    exec @result = xp_cmdshell 'D:\temp\Debug\ConsoleAppModule'

    Then you need to PIVOT those rights. See the articles about Cross-Tabs and Pivots in my signature line for how to PIVOT the data.

Viewing 3 posts - 1 through 2 (of 2 total)

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