Writing Japanese string to a text file through SQL query - sql-server

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.

Related

utf-8 path in master..xp_cmdshell command [duplicate]

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\'

xp_cmdshell command not executing last command when run as job

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.

Periodically check if a folder has been deleted, using MSSQL SP + Command Line?

I want to write an MSSQL Stored Procedure that periodically checks the filesystem it lives on to see if any folders have been deleted. It should work in an XP or Windows 7 environment.
I was thinking I might use the Windows Command Line or PowerShell (or VBScript) for this. I'll just call the script from the SQL Stored Procedure, it will check the filesystem, and then if a folder has been deleted it will alert the users.
My gut tells me there is a dead-simple solution for this somewhere. I know that matching directories is already a common task.
I've been playing with command line DIR and TREE, but so far they give me too much text. I really just want a simple list of folders that I can put into a small table in SQL. (I know that's overkill but it's what was requested.)
CREATE TABLE [dbo].[TABLEYOUCREATE] (
[dir] varchar(1000)
, [diroutput] varchar(1000)
)
GO
DECLARE #cmd varchar(8000)
SELECT #cmd = 'Dir "' + #path + '"'
INSERT INTO TABLEYOUCREATE(diroutput) EXEC master..xp_cmdshell #cmd

Caret symbol getting ignored when calling a sybase stored procedure through batch file

I am calling a Sybase Stored proc from a batch file. One of the parameters of the SP is a search pattern. When I pass a search pattern of 'a-zA-Z0-9_/.+-^^*' , the search pattern reaching the SP is ''a-zA-Z0-9_/.+-^*' . One carat at the end is missing. This I came to know since I am unloading the contents of all the SP parameters to a .txt file.
'SET searchpattern=
if [%5]==[] (
SET searchpattern='a-zA-Z0-9_/.+-^^*'
) ELSE ('
Call SP XYZ like-
EXEC XYZ(%searchpattern%)
Can someone please tell me how to ensure that whatever pattern is in the batch file goes to the SP unaltered
Carot is the escape character. For each carot, use two carots so that it escapes itself.
echo ^&
echo ^>
echo ^^
Double quoting the term is another option, if you can use that.

Executing a bat file inside a Stored Procedure using SQL server 2005

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:)

Resources