将varchar / varbinary的整个表列转换为xml

时间:2016-03-24 11:41:56

标签: sql sql-server xml sqlxml

我有一个表,其中一列是varbinary,实际上包含base64编码的字符串。

Table: Messages
Id        |   int
Payload   |   varbinary(MAX)

我可以使用

查看其内容
select cast(Payload as varchar(max)) from Messages

但是,我需要将整个列转换为XMl,因此不需要这些类型的转换,我可以将更长的字符串存储为XML。对于单个条目,我可以执行类似

的操作
select convert(xml, (select top 1 cast(payload as varchar(max)) 
                     from Messages
                    )
              ) as PayLoad 
for XML PATH('');

如何将整个表格转换为xml?

由于

2 个答案:

答案 0 :(得分:2)

这应该适合你:

CREATE TABLE #temp(ID INT,PayLoad VARBINARY(MAX));
INSERT INTO #temp VALUES
                 (1,CAST('AAQSkZJRgABAQEAYABgAAD/4RDmRXhpZg' AS VARBINARY(MAX)))
                ,(2,CAST('AAAAJAAAISodpAAQAAAABAAAIVJydAAEAAAASAAAQz' AS VARBINARY(MAX)));  

--simple output
SELECT * 
FROM #temp;

--casted output
SELECT ID,CAST(PayLoad AS VARCHAR(MAX)) AS PayLoad 
FROM #temp;

--AS XML
SELECT ID
      ,CAST(PayLoad AS VARCHAR(MAX)) AS PayLoad
FROM #temp 
FOR XML PATH('Row'),ROOT('root');

--EDIT: new SELECT
--As table with Payload as XML
SELECT ID
      ,(SELECT CAST(PayLoad AS VARCHAR(MAX)) FOR XML PATH('PayLoad'),TYPE) AS PayLoad
FROM #temp 

DROP TABLE #temp;

答案 1 :(得分:0)

如何使用 <pluginManagement> <plugins> <plugin> <groupId>org.eclipse.m2e</groupId> <artifactId>lifecycle-mapping</artifactId> <version>1.0.0</version> <configuration> <lifecycleMappingMetadata> <pluginExecutions> <pluginExecution> <pluginExecutionFilter> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <versionRange>${jacoco.version}</versionRange> <executions> <!-- Prepares the property pointing to the JaCoCo runtime agent which is passed as VM argument when Maven the Surefire plugin is executed. --> <execution> <id>pre-unit-test</id> <goals> <goal>prepare-agent</goal> </goals> <configuration> <!-- Sets the path to the file which contains the execution data. --> <destFile>${jacoco.ut.execution.data.file}</destFile> <!-- Sets the name of the property containing the settings for JaCoCo runtime agent. --> <propertyName>surefireArgLine</propertyName> </configuration> </execution> <!-- Ensures that the code coverage report for unit tests is created after unit tests have been run. --> <execution> <id>post-unit-test</id> <phase>test</phase> <goals> <goal>report</goal> </goals> <configuration> <!-- Sets the path to the file which contains the execution data. --> <dataFile>${jacoco.ut.execution.data.file}</dataFile> <!-- Sets the output directory for the code coverage report. --> <outputDirectory>${project.reporting.outputDirectory}/jacoco-ut</outputDirectory> </configuration> </execution> <!-- Prepares the property pointing to the JaCoCo runtime agent which is passed as VM argument when Maven the Failsafe plugin is executed. --> <execution> <id>pre-integration-test</id> <phase>pre-integration-test</phase> <goals> <goal>prepare-agent</goal> </goals> <configuration> <!-- Sets the path to the file which contains the execution data. --> <destFile>${jacoco.it.execution.data.file}</destFile> <!-- Sets the name of the property containing the settings for JaCoCo runtime agent. --> <propertyName>failsafeArgLine</propertyName> </configuration> </execution> <!-- Ensures that the code coverage report for integration tests after integration tests have been run. --> <execution> <id>post-integration-test</id> <phase>post-integration-test</phase> <goals> <goal>report</goal> </goals> <configuration> <!-- Sets the path to the file which contains the execution data. --> <dataFile>${jacoco.it.execution.data.file}</dataFile> <!-- Sets the output directory for the code coverage report. --> <outputDirectory>${project.reporting.outputDirectory}/jacoco-it</outputDirectory> </configuration> </execution> </executions> </pluginExecutionFilter> </pluginExecution> </pluginExecutions> </lifecycleMappingMetadata> </configuration> </plugin> </plugins> </pluginManagement>

for xml path

您可以添加相应的注释。您还可以拥有多个列。文档为here