在xp_sprintf中的Int占位符%d - SQL Server
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在xp_sprintf中的Int占位符%d - SQL Server相关的知识,希望对你有一定的参考价值。
我有一个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);
要求:我需要将Row转换为以下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 only supports string arguments and %s placeholders,因此无法在SQL Server中使用%d占位符。您必须将数字转换为字符串,然后使用它来填充%s占位符。
满足您的要求的最简单方法是使用内置的XML功能。
update [target]
SET [ScoreInfo] = [XmlValue]
FROM [dbo].[StudentMark] AS target
JOIN (
SELECT [StudentMarkId],
(
SELECT
[StudentMarkId],[StudentId], [SubjectId], [GeneratedOn], [Score], [IsPass]
FROM [dbo].[StudentMark] AS innr
WHERE outr.[StudentMarkId] = innr.[StudentMarkId]
FOR XML PATH('ScoreInfo'), TYPE
) as [XmlValue]
FROM [dbo].[StudentMark] AS outr
) AS source
ON target.[StudentMarkId] = source.[StudentMarkId]
将ScoreInfo设置为:
<ScoreInfo>
<StudentMarkId>%d</StudentMarkId>
<StudentId>%s</StudentId>
<SubjectId>%s</SubjectId>
<Score>%d</Score>
<GeneratedOn>%s</GeneratedOn>
<IsPass>%d</IsPass>
</ScoreInfo>
对于每一行,在一个查询中。
因为xp_sprintf只支持“%s”,所以需要将任何数值CAST到VARCHAR
修改您的示例如下:
DECLARE @ret VARCHAR(500) DECLARE @name VARCHAR(150) DECLARE @val VARCHAR(10) DECLARE @score INT
设置@name ='约翰' 设置@score = 75 设置@val = CAST(@score为VARCHAR(10))
EXEC master..xp_sprintf @ret OUTPUT,'Hello%s,你的分数是%s',@ name,@ val
打印@ret
以上是关于在xp_sprintf中的Int占位符%d - SQL Server的主要内容,如果未能解决你的问题,请参考以下文章