【发布时间】:2017-08-22 17:33:21
【问题描述】:
我有一个 XML 模板,我需要使用 xp_sprintf 根据表中的行值构造一个 XML。此外,该表具有 Int 和 Bit 值。
表格架构: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,并且需要在相应记录的列 [dbo].[StudentMark].[ScoreInfo] 中进行更新。
XML 模板:
<ScoreInfo>
<StudentMarkId>%d</StudentMarkId>
<StudentId>%s</StudentId>
<SubjectId>%s</SubjectId>
<Score>%d</Score>
<GeneratedOn>%s</GeneratedOn>
<IsPass>%d</IsPass>
</ScoreInfo>
我从 Stackoverflow 问题之一尝试了以下示例代码
declare @name varchar(150)
set @name = 'John'
declare @score int
set @score = 75
DECLARE @ret varchar(500)
exec master..xp_sprintf @ret OUTPUT, 'Hello %s, your score is %d', @name, @score
PRINT @ret
我收到以下错误消息
执行扩展存储过程时出错:无效的参数类型消息 50003,级别 1,状态 0
请帮助我如何使用指定的表[dbo].[StudentMark]构造模板XML
我提出了以下问题
请帮助我如何使用数字格式说明符等,
【问题讨论】:
-
为什么要使用 xp_sprintf?
-
你有什么替代功能吗?如果是,请帮助我...
-
你可以从阅读FOR XML (SQL Server)开始
-
@JamesZ - 好的,感谢您的帮助,但在此
xp_sprintf中,我们如何使用%d?你能澄清一下吗... -
你不能使用 %d,xp_sprintf 只支持 %s。您必须手动将@score 转换为字符串类型,然后将其与格式字符串“Hello %s, your score is %s”一起传递给 xp_sprintf。
标签: sql sql-server printf string-formatting format-specifiers