question

salleo avatar image
salleo asked

xp_cmdshell with variable in CMD

is tehere a way to put a variable into xp_cmdshell? example: DECLARE @command varchar (600) DECLARE @Newfilename varchar (50) SET @Newfilename = (select TOP (1) [DDTNR]+'.cmd' AS 'DDTNR' from [111].[dbo].[DDT_LFS]) SET @COMMAND = 'rename "\\9_Batch\EmptyFile.txt" @Newfilename' exec master.dbo.xp_cmdshell @command in this example the file EmptyFile.txt will be rename to @Newfilename but not as the Variable Content. thx salleo
tsqlvariablesxp-cmdshell
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Usman Butt avatar image
Usman Butt answered
You need to change the statement from SET @COMMAND = 'rename "\\9_Batch\EmptyFile.txt" @Newfilename' to something like SET @COMMAND = 'rename "\\9_Batch\EmptyFile.txt" "'+ @Newfilename+'"' See if it helps. But I am hoping that you already know the ill effects of turning on the "xp_cmdshell" component.
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

salleo avatar image salleo commented ·
thank you
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.