挑战你应该选择接受它......如果它真的可能的话。
我有一个存储过程,它以XML格式收集信息并将其放在表字段中。我现在需要选择此表字段并将其输出到.XML文件。
所以:
SELECT TOP 1 xmlfield FROM excelTest
我有另一个存储过程,它会将此作为附件发送给所选人。
我不能使用外部方法,即用Java或C#等编写的程序。 我不能使用sqlcmd,因为它在安全措施上被阻止了。 我使用的是SQl Server 2008,因此无法使用INTO或其他MySQL命令。
如果有人对我有任何解决方案,那就太棒了。
谢谢,
麦克
答案 0 :(得分:0)
很遗憾你不能使用sqlcmd。 无论如何,我会在这里留下一些信息,以防它帮助其他人。
要通过sqlcmd执行此操作,您需要做几件事。在sql中,您需要打开xml并打开nocount。然后在sqlcmd中指定输入和输出文件。 这是一个使用的sql的例子。我猜你可以把它放在一个存储过程中。 FOR XML是允许查询输出xml的关键。获得正确的嵌套非常棘手,但您可以在下面看到嵌套查询的示例。
SET NOCOUNT ON
:XML ON
select
client.clnum as CompanyKey,
client.clname1 as Name,
client.crelated as CompanyNumber,
client.clnum as ClientNumber,
(select ISNULl(client.clname2,'') as AlternateName from client c1 where c1.clnum=client.clnum for XML path
('AlternateNames'),type),
(select 'COMPANY' as Type,ISNULL(c2.clbusi,'') as [Description],'True' as IsDefault,
(
select
(
select
(select [Order], LineValue from (
select '1' as [Order], ISNULL(c4.claddr1,'') as LineValue from client c4 where c4.clnum=c3.clnum
union
select '2' as [Order], ISNULL(c4.claddr2,'') as LineValue from client c4 where c4.clnum=c3.clnum
union
select '3' as [Order], ISNULL(c4.claddr3,'') as LineValue from client c4 where c4.clnum=c3.clnum
union
select '4' as [Order], ISNULL(c4.claddr4,'') as LineValue from client c4 where c4.clnum=c3.clnum
union
select '5' as [Order], ISNULL(c4.claddr5,'') as LineValue from client c4 where c4.clnum=c3.clnum
union
select '6' as [Order], ISNULL(c4.claddr6,'') as LineValue from client c4 where c4.clnum=c3.clnum
) as x where LTRIM(rtrim(x.LineValue)) !='' for xml path ('Line'),type)
for xml path (''),type) as Lines,
ISNULL(c3.clcity,'') as City,ISNULL(c3.clstate,'') as State,ISNULL(c3.clcountry,'') as Country,ISNULL(c3.clzip,'') as PostalCode
from client c3 where c3.clnum=c2.clnum for xml path (''),type) as [Address]
from client c2 where c2.clnum=client.clnum for XML path ('CompanyLocation'),type)
as CompanyLocations
from client order by clnum
for xml path ('Company'),root ('Companies')
set nocount off
sqlcmd的示例。
sqlcmd -S <servername> -d <database> -E -i C:\\someinputpath\\Company.sql -o C:\\someoutputpath\\Company.xml
我很好奇并且无法真正找到答案是如何处理字符编码。
答案 1 :(得分:0)
如果您有权限,可以使用Ole Automation Procedures。这是一个示例脚本,它从选自TABLE变量的示例XML中创建C:\Temp\breakfast.xml
。
漂亮不是,如果其他选项不实用或不允许,请使用它。
DECLARE @excel_test TABLE(xml_field XML);
INSERT INTO @excel_test(xml_field)VALUES(
'<breakfast_menu>
<food>
<name>Belgian Waffles</name>
<price>$5.95</price>
<description>Two of our famous Belgian Waffles with plenty of real maple syrup</description>
<calories>650</calories>
</food>
<food>
<name>Strawberry Belgian Waffles</name>
<price>$7.95</price>
<description>Light Belgian waffles covered with strawberries and whipped cream</description>
<calories>900</calories>
</food>
</breakfast_menu>');
DECLARE @data VARBINARY(MAX);
SET @data=(SELECT TOP 1 CAST(xml_field AS VARBINARY(MAX)) FROM @excel_test);
DECLARE @file_path VARCHAR(256) = 'C:\Temp\breakfast.xml';
DECLARE @hr INT;
DECLARE @object_token INT;
EXEC @hr=sp_OACreate 'ADODB.Stream', @object_token OUTPUT; -- Create instance
IF @hr<>0 GOTO __fail;
EXEC @hr=sp_OASetProperty @object_token, 'Type', 1;
IF @hr<>0 GOTO __cleanup;
EXEC @hr=sp_OAMethod @object_token, 'Open'; -- Open the stream
IF @hr<>0 GOTO __cleanup;
EXEC @hr=sp_OAMethod @object_token, 'Write', NULL, @data; -- Write data to the stream
IF @hr<>0 GOTO __close;
EXEC sp_OAMethod @object_token, 'SaveToFile', NULL, @file_path, 2; -- Save the stream to file
__close:
EXEC sp_OAMethod @object_token, 'Close'; -- Close the stream
__cleanup:
EXEC sp_OADestroy @object_token; -- Destroy the instance
__fail:
要启用Ole Automation Procedures
(如果您当然允许),请先运行此操作以重新配置设置:
sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'Ole Automation Procedures', 1;
GO
RECONFIGURE;
GO