[dba-SQLServer] Passing variable to xp cmdshell

Francisco Tapia fhtapia at gmail.com
Wed Apr 15 09:59:37 CDT 2009


I've always pushed my results to a table and not a variable... let me dig up
what i've done before

     SET @fPath = 'dir "myserver\myinitialfolder" / b'
    SET @sCommand = @fPath
    INSERT INTO #tmptable (Field2)
    EXEC master..xp_cmdshell @sCommand


-Francisco
http://sqlthis.blogspot.com | Tsql and More...


On Wed, Apr 15, 2009 at 1:32 AM, Paul Hartland <paul.hartland at googlemail.com
> wrote:

> To all,
>
> I am having a problem using xp cmdshell in SQL Server 2005, I won't go into
> too much detail but basically I am just trying to put a folder list into a
> temporary table.
>
> if I use exec master..xp cmdshell 'dir "myserver\myinitialfolder" /b'  it
> works fine, however I need to generate the folder on the fly, but if I
> declare a variable say @folder varchar(4000) and then have exec master..xp
> cmdshell @folder I get
>
> Msg 214, Level 16, State 201, Procedure xp cmdshell, Line 1
> Procedure expects parameter 'command_string' of type 'varchar'.
>
> Anyone any ideas whats happening, or what I am doing wrong ?
>
> Thanks is advance for any help on this.
>
> --
> Paul Hartland
> paul.hartland at googlemail.com
> _______________________________________________
> dba-SQLServer mailing list
> dba-SQLServer at databaseadvisors.com
> http://databaseadvisors.com/mailman/listinfo/dba-sqlserver
> http://www.databaseadvisors.com
>
>



More information about the dba-SQLServer mailing list