exec xp_cmdshell 'dir d:\ftproot\transfin\"+@stcode+"' what is wrong
- User
exec xp_cmdshell 'dir d:\ftproot\transfin*"+@stcode+"*'
what is wrong
- Assistant
The command you provided is attempting to execute a shell command using SQL Server's
xp_cmdshellto list files in a specific directory that match a certain pattern. However, there's a problem with how the variable@stcodeis being concatenated into the command string.In T-SQL, you cannot concatenate variables directly inside a string like you're attempting to do. Instead, you need to build the command string first and then execute it. Here's how you can do that:
- Declare your variable
@stcode. - Build the command string using string concatenation.
- Execute the final command string.
Here is a modified version of your code:
sqlDECLARE @stcode NVARCHAR(50); -- Adjust the size as necessary DECLARE @cmd NVARCHAR(255); SET @stcode = 'YourStCodeValue'; -- set your actual value here SET @cmd = 'dir d:\ftproot\transfin\*' + @stcode + '*'; EXEC xp_cmdshell @cmd; - Declare your variable
- User
declare @stcode varchar(max), @cmd varchar(max) select @stcode = STCODE from estore..pospara set @cmd = 'dir d:\ftproot\transfin*' + @stcode + '*'
exec xp_cmdshell @cmd
- Assistant
- User
Msg 214, Level 16, State 201, Procedure xp_cmdshell, Line 1 Procedure expects parameter 'command_string' of type 'varchar'.
- Assistant
The error message you received (
Msg 214, Level 16, State 201... Procedure expects parameter 'command_string' of type 'varchar') indicates that the command string being passed toxp_cmdshellis not being recognized as a valid type. This can happen if the command string is being built as or if there are issues with the content of the string that could lead to it not being treated as a valid .