declaring variables, with procedures... whats the deal?

  • if i wanted to do this:

    declare @output

    set @output = (xp_cmdshell "type C:\textfile.txt")

    now this flags an error, but not sure why.

    what am i doing wrong?

    _________________________

  •  

    set nocount on

    declare @output varchar(8000)

    create table #output  (outputtext varchar(8000))

    insert into #output

    exec master..xp_cmdshell "type C:\test.txt"

    select @output=outputtext from #output

    drop table #output

    print @output

    -Krishnan

  • thanks krishnan

    just tested this out... i'm not getting any ouput

    at all. i'm not seeing the results from the text.txt

    file.

    this file by the way... just is an error log, and was

    hoping to see the text as the output.

    is there some thing else that needs to be done?

    just wondering.

    thanks again for your help.

    _________________________

  • this doesn't go for all procedures in variables right?? is there another way?

    _________________________

  • Hi neotokio,

    Krishnan's solution worked for me.  There are 2 things:

    1. select @output=outputtext from #output

    line just assigns the text to the variable @output. It does not print it out. you have to have another line with Select @output to see on-screen. OR just replace this line with Select * from #output.

    2. If the file name / path is not correct the only thing that the #output table will contain is an error message that it could not find a file.

    3. from your original post why your sp_cmdshell statement did not return anything and you got an error message, it is because it has to be preceeded with Exec keyword like in Krishnan's example.

    4 as it was in your original post

     set @output = (xp_cmdshell "type C:\textfile.txt")

    this @output will return a status of SP execution (return status), not a result set. But the syntax for getting a return status should be

    Execute @output = (xp_cmdshell "type C:\textfile.txt")

     

     

    Regards,Yelena Varsha

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

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