I have a query that generates XML files and loads them to FTP with .
I need to switch encoding to UTF-8 as follows:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[MyXMLTest]
@FileDestinationDir VARCHAR(2000)
-- to call procedure specify your own file path
-- EXEC [Audit_DBA].[dbo].[MyXMLTest] 'E:\NLP\GovwinIQ_Ontology\NewFolder'
AS
SET QUOTED_IDENTIFIER ON
IF OBJECT_ID (N'InputTemp.dbo.XMLTest', N'U') IS NOT NULL
DROP TABLE InputTemp.dbo.XMLTest;
CREATE TABLE InputTemp.dbo.XMLTest
(
[Id] INT NOT NULL,
[FirstName] VARCHAR(100) NOT NULL,
[LastName] VARCHAR(100) NOT NULL,
[Address] VARCHAR(100) NOT NULL
);
INSERT INTO InputTemp.dbo.XMLTest ([Id], [FirstName], [LastName], [Address])
VALUES (12, 'Zhuk', 'Termik', '123 Gam Str, Boston, NY');
--SELECT * FROM InputTemp.dbo.XMLTest
DECLARE @FilePath VARCHAR(4000)
DECLARE @SQLStr NVARCHAR(4000),
@Cmd NVARCHAR(4000),
@Ret INT
DECLARE @Id INT;
SELECT @Id = 12;
SELECT @SQLStr =
'SELECT N'''' + (SELECT CAST((SELECT [Id], [FirstName], [LastName], [Address] FROM InputTemp.dbo.XMLTest AS Body WHERE Id = ''' + str(@Id) + ''' FOR XML AUTO, ELEMENTS) AS NVARCHAR(MAX)))'
SELECT @SQLStr AS SQLStr
SELECT @FilePath = @FileDestinationDir+'\NewFolder'+ltrim(rtrim(str(@Id)))+'.xml'
SELECT @Cmd = ' bcp " ' + @SQLStr + '" queryout '+@FilePath+' -c -C65001 -r "" -T -S ' +@@ServerName
EXEC @Ret = master.dbo.xp_cmdshell @Cmd
IF OBJECT_ID (N'InputTemp.dbo.XMLTest', N'U') IS NOT NULL
DROP TABLE InputTemp.dbo.XMLTest;
GO