This question already has answers here:
What is the meaning of the prefix N in T-SQL statements and when should I use it?
(4 answers)
Closed 9 months ago.
How can I execute master..xp_cmdshell in SQL Server with path that includes utf-8 chars
the code works well in another servers and in cmd but not in sql
EXEC master..xp_cmdshell 'dir \\my-drive\users\myUser\utf8_folder_name\'
Pay attention to the path:
\\my-drive\users\myUser\utf8_folder_name\
when I call to upper folder - without utf-8 chars but same security, the command works correctly
(\\my-drive\users\myUser\ -no utf8 chars)
I also tried to wrap the path with quotation
The error is:
output
The filename, directory name, or volume label syntax is incorrect.
NULL
Add N before
EXEC master..xp_cmdshell N'dir \\my-drive\users\myUser\utf8_folder_name\'
Related
I have a requirement where i need to write Japanese strings stored in a table to a text file.
I tried below query, its printing only question marks in text file.
DECLARE #cmd NVARCHAR(2000), #Msg NVARCHAR(1000);
SELECT #Msg=String FROM JapaneseStringTable;
SET #cmd='echo '+#Msg+'>>'+'C:\Data\Test.txt';
EXEC sys.xp_cmdshell #cmd;
I tried to hard code the Japanese string and write to file, still no luck
DECLARE #cmd NVARCHAR(2000),
#Msg NVARCHAR(1000)=N'データノード';
SET #cmd='echo '+#Msg+'>>'+'C:\Data\Test.txt';
EXEC sys.xp_cmdshell #cmd;
This is because your OS has a different code page than Japanese. So your problem is not related to SQL Server that passes Japanese string, but to OS that operates this string as ASCII.
To get the correct result you should change OS codepage in someway.
You can prove it to yourself by using your command directly from cmd:
echo データノード >> C:\Data\Test.txt
And this will give you ?????
If instead you first change codepage in the same cmd window:
chcp 936
and then execute your echo データノード >> C:\Data\Test.txt
output file will have correct ascii codes.
If you still cannot see them correctly use Word to view this file, but at least there will be no character lost (?????) and ascii codes will be correct.
First off, before everybody shouts at me - I'm bug fixing in legacy code and a re-write is off the cards for now - I have to try to find a fix using the xp_cmdshell command.
I have a proc which is executed via a scheduled job. The proc is full of TSQL like the below to dump data to a log file.
SELECT *
INTO Temp
FROM MyView
SET #cmd1 = 'bcp "SELECT * FROM [myDatabase].dbo.Temp" queryout "C:\temp.txt" -T -c -t" "'
SET #cmd2= 'type "C:\temp.txt" >> "C:\output.txt"'
EXEC master..xp_cmdshell #cmd1
EXEC master..xp_cmdshell #cmd2
DROP TABLE Temp
The problem is that the last of these commands in the proc doesn't appear to run. I can see the result in the text.txt file, but not the output.txt file. All of the preceding work fine though and it works fine when I run this on it's own.
Can anyone suggest why this might happen or suggest an alternative way to achieve this?
Thanks
I think, that BCP as external process runs async. So it could be, that your file is not yet written in the moment you are trying to copy its content.
Suggestion 1: Include an appropriate wait time
Suggestion 2: Call your first command a second time with changed target file name
Suggestion 3: Use copy rather than type
You might create a file c\temp.txt with just hello world in it. Try to type it into one file before the BCP and type it into another file after the BCP.
I am somewhat new at this and I am having trouble. I am calling a CMD file that then calls a SQL script to execute some database commands.
The relevant CMD file code is:
set days = '360'
sqlplus #\file.sql %days%
The SQL script excerpt is:
define numOfDays = &1
EXEC (numOfDays, .....)
When executed, I get an error saying the numOfDays variable is undeclared. FYI, this is a Windows OS with a Oracle database. Any help would be appreciated and more info can be provided if needed. Thanks.
EDIT:
Added the '%' signs around the 'days' parameter that were accidentally left out.
You can try by surrounding the param with %.
e.g:
SET days=360
sqlplus #\file.sql %days%
This is how bat/cmd files recognize the variables.
This question already has answers here:
Ignore percent sign in batch file
(6 answers)
Closed 2 years ago.
Hi Guys having a really hard time getting an environmental variable piped to clip.exe, in a regular command prompt it works but once in batch file it fails. I have tried escaping with double %% and a few other tricks. Nothing seems to work.
I am trying to pipe the output of the citrix environmental variable %clientname% to clip.exe like the following:
#echo off
echo %clientname% | clip
Just use the ^ sign to escape the % char in command line:
C:\> echo ^%clientname^%
But you need to use the % sign to escape a % symbol in a batch file:
#echo %%clientname%%
When i try to execute a bat file using xp_CMDShell, i am getting a message as not recognized command.
Following is the command i executed:
EXEC master..xp_CMDShell 'C:\Documents and Settings\adcxqcv\Desktop\PQA\sample.bat'
I got a message as follows:
'C:\Documents' is not recognized as an internal or external command,
operable program or batch file.
NULL
Any Suggestions. Let me know how to execute a bat file inside a Stored Procedure.
I am new to SQl Server.
Thanks,
Vinu
Put the path inside ""
EXEC master..xp_CMDShell '"C:\Documents and Settings\adcxqcv\Desktop\PQA\sample.bat"'
xp_cmdshell can be a bit picky on the long file names, you are using quotes and it isn't playing ball, double quotes can sometimes work but if it still doesn't want to play ball then try use the older 8.3 filename instead.
exec master..xp_cmdshell 'c:\docume~1\adcxqcv\Desktop\PQA\sample.bat'
Without parameter
exec(' xp_cmdshell ''C:\script\test.bat'); --your bat file location(path)
With parameter
exec(' xp_cmdshell ''C:\script\test.bat '+#ecistate+' '+#stateid+' '+#pcno+''''); --your bat file location(path)
Execute and enjoy the solution:)