对于 SQL 服务器中的 XML 自定义属性和元素
For XML Custom Attribute and Element in SQL Server
我需要创建以下 XML 自定义元素和使用硬编码数据的属性。
Table 架构:StudentMark:
CREATE TABLE [dbo].[StudentMark]
(
[StudentMarkId] [int] IDENTITY(1,1) NOT NULL,
[StudentId] [uniqueidentifier] NOT NULL,
[SubjectId] [uniqueidentifier] NOT NULL,
[Score] [int] NOT NULL,
[ScoreInfo] [xml] NOT NULL,
[GeneratedOn] [datetime2](2) NOT NULL,
[IsPass] [bit] NOT NULL,
CONSTRAINT [PK_StudentMark]
PRIMARY KEY CLUSTERED ([StudentMarkId] ASC)
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
样本种子数据
INSERT INTO [dbo].[StudentMark] ([StudentId], [SubjectId], [ScoreInfo], [GeneratedOn], [Score], [IsPass])
VALUES ('FC3CB475-B480-4129-9190-6DE880E2D581', '0D72F79E-FB48-4D3E-9906-B78A9D105081', '<StudentMarkAttribute xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"></StudentMarkAttribute>', '2017-08-10 10:10:15', 95, 1),
('0F4EF48C-93E3-41AA-8295-F6B0E8D8C3A2', '0D72F79E-FB48-4D3E-9906-B78A9D105081', '<StudentMarkAttribute xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"></StudentMarkAttribute>', '2017-08-10 10:10:15', 100, 1),
('0F4EF48C-93E3-41AA-8295-F6B0E8D8C3A2', 'AB172272-D2E9-49E1-8040-6117BB6743DB', '<StudentMarkAttribute xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"></StudentMarkAttribute>', '2017-08-16 09:06:20', 25, 0),
('FC3CB475-B480-4129-9190-6DE880E2D581', 'AB172272-D2E9-49E1-8040-6117BB6743DB', '<StudentMarkAttribute xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"></StudentMarkAttribute>', '2017-08-16 09:06:20', 82, 1);
要求:我需要在XML中添加一个元素<DigitallySigned>
,XML是使用where子句的一个Record WHERE [StudentMarkId] = 1
。请参阅以下模板,属性 DateSigned
与元素 GeneratedOn
完全相同 dateTime
并且元素 Admin
的值是硬编码值。
XML 模板:
<ScoreInfo>
<DigitallySigned DateSigned="2017-08-10 10:10:15">Admin</DigitallySigned>
<StudentMarkId>1</StudentMarkId>
<StudentId>FC3CB475-B480-4129-9190-6DE880E2D581</StudentId>
<SubjectId>0D72F79E-FB48-4D3E-9906-B78A9D105081</SubjectId>
<Score>95</Score>
<GeneratedOn>2017-08-10 10:10:15</GeneratedOn>
<IsPass>1</IsPass>
</ScoreInfo>
我尝试了以下代码
SELECT TOP (1)
(SELECT SM.[GeneratedOn] AS [DateSigned] FOR XML RAW)
,SM.[StudentMarkId]
,SM.[StudentId]
,SM.[SubjectId]
,SM.[Score]
,SM.[GeneratedOn]
,SM.[IsPass]
FROM [DevDB].[dbo].[StudentMark] SM
WHERE SM.[StudentMarkId] = 1
FOR XML RAW ('ScoreInfo'), ELEMENTS
我得到以下 XML 输出
<ScoreInfo><row DateSigned="2017-08-10T10:10:15"/><StudentMarkId>1</StudentMarkId><StudentId>FC3CB475-B480-4129-9190-6DE880E2D581</StudentId><SubjectId>0D72F79E-FB48-4D3E-9906-B78A9D105081</SubjectId><Score>95</Score><GeneratedOn>2017-08-10T10:10:15</GeneratedOn><IsPass>1</IsPass></ScoreInfo>
请帮助我如何获得所需的XML
。
开始了。
SELECT TOP (1)
[GeneratedOn] AS "DigitallySigned/@DateSigned",
'Admin' AS "DigitallySigned"
,SM.[StudentMarkId]
,SM.[StudentId]
,SM.[SubjectId]
,SM.[Score]
,SM.[GeneratedOn]
,SM.[IsPass]
FROM [DevDB].[dbo].[StudentMark] SM
WHERE SM.[StudentMarkId] = 1
FOR XML PATH ('ScoreInfo');
我需要创建以下 XML 自定义元素和使用硬编码数据的属性。
Table 架构:StudentMark:
CREATE TABLE [dbo].[StudentMark]
(
[StudentMarkId] [int] IDENTITY(1,1) NOT NULL,
[StudentId] [uniqueidentifier] NOT NULL,
[SubjectId] [uniqueidentifier] NOT NULL,
[Score] [int] NOT NULL,
[ScoreInfo] [xml] NOT NULL,
[GeneratedOn] [datetime2](2) NOT NULL,
[IsPass] [bit] NOT NULL,
CONSTRAINT [PK_StudentMark]
PRIMARY KEY CLUSTERED ([StudentMarkId] ASC)
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
样本种子数据
INSERT INTO [dbo].[StudentMark] ([StudentId], [SubjectId], [ScoreInfo], [GeneratedOn], [Score], [IsPass])
VALUES ('FC3CB475-B480-4129-9190-6DE880E2D581', '0D72F79E-FB48-4D3E-9906-B78A9D105081', '<StudentMarkAttribute xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"></StudentMarkAttribute>', '2017-08-10 10:10:15', 95, 1),
('0F4EF48C-93E3-41AA-8295-F6B0E8D8C3A2', '0D72F79E-FB48-4D3E-9906-B78A9D105081', '<StudentMarkAttribute xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"></StudentMarkAttribute>', '2017-08-10 10:10:15', 100, 1),
('0F4EF48C-93E3-41AA-8295-F6B0E8D8C3A2', 'AB172272-D2E9-49E1-8040-6117BB6743DB', '<StudentMarkAttribute xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"></StudentMarkAttribute>', '2017-08-16 09:06:20', 25, 0),
('FC3CB475-B480-4129-9190-6DE880E2D581', 'AB172272-D2E9-49E1-8040-6117BB6743DB', '<StudentMarkAttribute xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"></StudentMarkAttribute>', '2017-08-16 09:06:20', 82, 1);
要求:我需要在XML中添加一个元素<DigitallySigned>
,XML是使用where子句的一个Record WHERE [StudentMarkId] = 1
。请参阅以下模板,属性 DateSigned
与元素 GeneratedOn
完全相同 dateTime
并且元素 Admin
的值是硬编码值。
XML 模板:
<ScoreInfo>
<DigitallySigned DateSigned="2017-08-10 10:10:15">Admin</DigitallySigned>
<StudentMarkId>1</StudentMarkId>
<StudentId>FC3CB475-B480-4129-9190-6DE880E2D581</StudentId>
<SubjectId>0D72F79E-FB48-4D3E-9906-B78A9D105081</SubjectId>
<Score>95</Score>
<GeneratedOn>2017-08-10 10:10:15</GeneratedOn>
<IsPass>1</IsPass>
</ScoreInfo>
我尝试了以下代码
SELECT TOP (1)
(SELECT SM.[GeneratedOn] AS [DateSigned] FOR XML RAW)
,SM.[StudentMarkId]
,SM.[StudentId]
,SM.[SubjectId]
,SM.[Score]
,SM.[GeneratedOn]
,SM.[IsPass]
FROM [DevDB].[dbo].[StudentMark] SM
WHERE SM.[StudentMarkId] = 1
FOR XML RAW ('ScoreInfo'), ELEMENTS
我得到以下 XML 输出
<ScoreInfo><row DateSigned="2017-08-10T10:10:15"/><StudentMarkId>1</StudentMarkId><StudentId>FC3CB475-B480-4129-9190-6DE880E2D581</StudentId><SubjectId>0D72F79E-FB48-4D3E-9906-B78A9D105081</SubjectId><Score>95</Score><GeneratedOn>2017-08-10T10:10:15</GeneratedOn><IsPass>1</IsPass></ScoreInfo>
请帮助我如何获得所需的XML
。
开始了。
SELECT TOP (1)
[GeneratedOn] AS "DigitallySigned/@DateSigned",
'Admin' AS "DigitallySigned"
,SM.[StudentMarkId]
,SM.[StudentId]
,SM.[SubjectId]
,SM.[Score]
,SM.[GeneratedOn]
,SM.[IsPass]
FROM [DevDB].[dbo].[StudentMark] SM
WHERE SM.[StudentMarkId] = 1
FOR XML PATH ('ScoreInfo');