May 22, 2012 at 4:59 am
Hi,
I have the following code:
exec master.dbo.xp_cmdshell 'rmdir "c:\tempo_SGCTLOCAL\"', no_output
exec master.dbo.xp_cmdshell 'mkdir "c:\tempo_SGCTLOCAL\"', no_output
exec master.dbo.xp_cmdshell 'MOVE '+@caminho+'*.* c:\tempo_SGCTLOCAL', no_output
exec master.dbo.xp_cmdshell 'rd /s/q '+@caminho+'', no_output
exec master.dbo.xp_cmdshell 'mkdir "'+@caminho+'"', no_output
exec master.dbo.xp_cmdshell 'MOVE c:\tempo_SGCTLOCAL*.rar '+@caminho+'', no_output
exec master.dbo.xp_cmdshell 'rd /s/q c:\tempo_sgrs', no_output
When I try to parse it, i receive the following error:
Msg 102, Level 15, State 1, Procedure sp_SGCT_APAGAFICHEIROS_SGCTLOCAIS, Line 13
Incorrect syntax near '+'.
Msg 102, Level 15, State 1, Procedure sp_SGCT_APAGAFICHEIROS_SGCTLOCAIS, Line 14
Incorrect syntax near '+'.
Msg 102, Level 15, State 1, Procedure sp_SGCT_APAGAFICHEIROS_SGCTLOCAIS, Line 15
Incorrect syntax near '+'.
Msg 102, Level 15, State 1, Procedure sp_SGCT_APAGAFICHEIROS_SGCTLOCAIS, Line 16
Incorrect syntax near '+'.
Msg 102, Level 15, State 1, Procedure sp_SGCT_APAGAFICHEIROS_SGCTLOCAIS, Line 25
Incorrect syntax near '+'.
Msg 102, Level 15, State 1, Procedure sp_SGCT_APAGAFICHEIROS_SGCTLOCAIS, Line 27
Incorrect syntax near '+'.
How can I put the value of the variable @caminho inside the '' without receiving this error?
Casn someone help?
Thank you.
P.S - I know about the security risks of using the xp_cmdshell.
May 22, 2012 at 6:28 am
But how to do it for several at the same time, like:
exec master.dbo.xp_cmdshell 'rmdir "c:\tempo_SGCTLOCAL\"', no_output
exec master.dbo.xp_cmdshell 'mkdir "c:\tempo_SGCTLOCAL\"', no_output
exec master.dbo.xp_cmdshell 'MOVE '+@caminho+'*.* c:\tempo_SGCTLOCAL', no_output
exec master.dbo.xp_cmdshell 'rd /s/q '+@caminho+'', no_output
exec master.dbo.xp_cmdshell 'mkdir "'+@caminho+'"', no_output
exec master.dbo.xp_cmdshell 'MOVE c:\tempo_SGCTLOCAL*.rar '+@caminho+'', no_output
exec master.dbo.xp_cmdshell 'rd /s/q c:\tempo_sgrs', no_output
I don't have only
exec master.dbo.xp_cmdshell 'rmdir "c:\tempo_SGCTLOCAL\"', no_output
May 22, 2012 at 6:33 am
easy, just set @sql = to what you want it to do then execute it like this
declare @sql nvarchar(max)
set @sql = 'exec master.dbo.xp_cmdshell ''rmdir "c:\tempo_SGCTLOCAL\"'', no_output'
exec (@sql)
set @sql = 'exec master.dbo.xp_cmdshell ''mkdir "c:\tempo_SGCTLOCAL\"'', no_output'
exec (@sql)
set @sql = 'exec master.dbo.xp_cmdshell ''MOVE ''+@caminho+''*.* c:\tempo_SGCTLOCAL'', no_output'
exec (@sql)
set @sql = 'exec master.dbo.xp_cmdshell ''rd /s/q ''+@caminho+'''', no_output'
exec (@sql)
set @sql = 'exec master.dbo.xp_cmdshell ''mkdir "''+@caminho+''"'', no_output'
exec (@sql)
set @sql = 'exec master.dbo.xp_cmdshell ''MOVE c:\tempo_SGCTLOCAL*.rar ''+@caminho+'''', no_output'
exec (@sql)
set @sql = 'exec master.dbo.xp_cmdshell ''rd /s/q c:\tempo_sgrs'', no_output'
exec (@sql)
May 22, 2012 at 6:35 am
Or do it in one go -
DECLARE @sql NVARCHAR(4000);
SELECT @sql =
'exec master.dbo.xp_cmdshell ''rmdir "c:\tempo_SGCTLOCAL\"'', no_output;'+CHAR(13)+CHAR(10)+
'exec master.dbo.xp_cmdshell ''mkdir "c:\tempo_SGCTLOCAL\"'', no_output;'+CHAR(13)+CHAR(10)+
'exec master.dbo.xp_cmdshell ''MOVE '+@caminho+'*.* c:\tempo_SGCTLOCAL'', no_output;'+CHAR(13)+CHAR(10)+
'exec master.dbo.xp_cmdshell ''rd /s/q '+@caminho+''', no_output;'+CHAR(13)+CHAR(10)+
'exec master.dbo.xp_cmdshell ''mkdir "'+@caminho+'"'', no_output;'+CHAR(13)+CHAR(10)+
'exec master.dbo.xp_cmdshell ''MOVE c:\tempo_SGCTLOCAL*.rar '+@caminho+''', no_output;'+CHAR(13)+CHAR(10)+
'exec master.dbo.xp_cmdshell ''rd /s/q c:\tempo_sgrs'', no_output;';
EXECUTE sp_executesql @sql;
Viewing 5 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply