Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 create file encoding problem xp_cmdshell

Author  Topic 

aliemrei
Starting Member

6 Posts

Posted - 2012-08-23 : 09:55:58
Hi,

DECLARE @Command nvarchar(4000), @text nvarchar(4000)
SET @text = 'öçsigü' -- it's turkish charset
SET @Command = ' echo '+@text+' > a:\tst.fr3 '
EXEC xp_cmdshell @Command

and file output is ”‡Ÿi§�

how can i set utf-8 encode for create a file by xp_cmdshell ?

thanks.

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2012-08-23 : 10:02:08
To start with you might need
SET @text = N'öçsigü'


==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

aliemrei
Starting Member

6 Posts

Posted - 2012-08-23 : 10:34:03
Thanks for reply, i tried but result is same. :(

DECLARE @Command nvarchar(4000), @text nvarchar(4000)
SET @text = N'öçsigü'
SET @Command = ' echo '+@text+' > a:\tst.fr3 '
EXEC xp_cmdshell @Command
Go to Top of Page

aliemrei
Starting Member

6 Posts

Posted - 2012-08-24 : 02:20:08
please, where are sqlteam.com's connoisseurs, there is not someone who knows?
Go to Top of Page

lionofdezert
Aged Yak Warrior

885 Posts

Posted - 2012-08-24 : 03:26:02
possibly its OS related issue as you need Portuguese for your operating system. If you change your output file type to DOC and on open it with Portuguese language support, then you can find it correct. It means, its just OS issue and not the collation problem on SQL Server side.

--------------------------
http://connectsql.blogspot.com/
Go to Top of Page

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2012-08-24 : 04:15:34
I tried to post this yesterday but the site didn';t like it
It gives me the correct result - i.e. what got written is what is read.
Note that sql server has removed utf-8 support from some utilities so yoou might need to use a client app like ssis if yoou need that format.

DECLARE @Command nvarchar(4000), @text nvarchar(4000)
SET @text = N'öçsigü'
SET @Command = ' echo '+@text+' > a:\tst.fr3 '
EXEC xp_cmdshell @Command

exec xp_cmdshell 'type a:\tst.fr3'


==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

aliemrei
Starting Member

6 Posts

Posted - 2012-08-24 : 05:35:58
@nigelrivett thanks for all. you are right. if use "exec xp_cmdshell 'type a:\tst.fr3'" command, charset is correct but i am looking file content and chars is "”‡Ÿi§" :(


thanks @lionofdezert for additional information. but i am using turkish OS but server's OS is english. I must look at the file content by notepad or etc.
Go to Top of Page

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2012-08-24 : 05:40:56
Then the issue is probably wit hthe client rather than the content of the file. You need to get something that is compable of reading the file format - the other option is to convert the file format (e.g. with ssis).

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

aliemrei
Starting Member

6 Posts

Posted - 2012-08-24 : 10:42:47
himm, I think, must find another file management method. :(
Go to Top of Page

aliemrei
Starting Member

6 Posts

Posted - 2012-08-26 : 03:49:28
resolve:

for create file :

Create Proc UCreateTextFile(@Filename VarChar(100),@Text nText)
AS
DECLARE @FileSystem int
DECLARE @FileHandle int
DECLARE @RetCode int


EXECUTE @RetCode = sp_OACreate 'Scripting.FileSystemObject' , @FileSystem OUTPUT
IF (@@ERROR|@RetCode > 0 Or @FileSystem < 0)
RAISERROR ('could not create FileSystemObject',16,1)

EXECUTE @RetCode = sp_OAMethod @FileSystem , 'OpenTextFile' , @FileHandle OUTPUT , @Filename, 2, 1
IF (@@ERROR|@RetCode > 0 Or @FileHandle < 0)
RAISERROR ('could not create File',16,1)

EXECUTE @RetCode = sp_OAMethod @FileHandle , 'Write' , NULL , @text
IF (@@ERROR|@RetCode > 0 )
RAISERROR ('could not write to File',16,1)

EXECUTE @RetCode = sp_OAMethod @FileHandle , 'Close'
IF (@@ERROR|@RetCode > 0)
RAISERROR ('Could not close file ',16,1)

EXEC sp_OADestroy @filehandle
IF (@@ERROR|@RetCode > 0)
RAISERROR ('Could not destroy file object',16,1)

EXEC sp_OADestroy @FileSystem
Go


for read file

CREATE FUNCTION [dbo].[ufsReadfileAsString]
(
@Path VARCHAR(255),
@Filename VARCHAR(100)
)
RETURNS
Varchar(max)
AS
BEGIN

DECLARE @objFileSystem int
,@objTextStream int,
@objErrorObject int,
@strErrorMessage Varchar(1000),
@Command varchar(1000),
@Chunk Varchar(8000),
@String varchar(max),
@hr int,
@YesOrNo int

Select @String=''
select @strErrorMessage='opening the File System Object'
EXECUTE @hr = sp_OACreate 'Scripting.FileSystemObject' , @objFileSystem OUT


if @HR=0 Select @objErrorObject=@objFileSystem, @strErrorMessage='Opening file "'+@path+'\'+@filename+'"',@command=@path+'\'+@filename

if @HR=0 execute @hr = sp_OAMethod @objFileSystem , 'OpenTextFile'
, @objTextStream OUT, @command,1,false,0--for reading, FormatASCII

WHILE @hr=0
BEGIN
if @HR=0 Select @objErrorObject=@objTextStream,
@strErrorMessage='finding out if there is more to read in "'+@filename+'"'
if @HR=0 execute @hr = sp_OAGetProperty @objTextStream, 'AtEndOfStream', @YesOrNo OUTPUT

IF @YesOrNo<>0 break
if @HR=0 Select @objErrorObject=@objTextStream,
@strErrorMessage='reading from the output file "'+@filename+'"'
if @HR=0 execute @hr = sp_OAMethod @objTextStream, 'Read', @chunk OUTPUT,4000
SELECT @String=@string+@chunk
end
if @HR=0 Select @objErrorObject=@objTextStream,
@strErrorMessage='closing the output file "'+@filename+'"'
if @HR=0 execute @hr = sp_OAMethod @objTextStream, 'Close'


if @hr<>0
begin
Declare
@Source varchar(255),
@Description Varchar(255),
@Helpfile Varchar(255),
@HelpID int

EXECUTE sp_OAGetErrorInfo @objErrorObject,
@source output,@Description output,@Helpfile output,@HelpID output
Select @strErrorMessage='Error whilst '
+coalesce(@strErrorMessage,'doing something')
+', '+coalesce(@Description,'')
select @String=@strErrorMessage
end
EXECUTE sp_OADestroy @objTextStream
-- Fill the table variable with the rows for your result set

SET @String = REPLACE(@String, ' Connected="True" ', 'Connected="False" ')
RETURN @string
END

GO
Go to Top of Page
   

- Advertisement -