我有一个如下查询:
SELECT
a.Name as 'Name of the program',
a.ContractCode as Code,
case
when a.IsActive = 1 THEN 'YES'
when a.IsActive = 0 THEN 'NO'
end as ACTIVE,
a.StartDate as 'Contract Start Date',
a.EndDate as 'Contract End Date',
case
when a.Description IS NULL THEN ''
else a.Description
End as 'Description',
b.Name as 'Service provider’s name',
a.CountryISO2 as 'Country',
c.DisplayName as 'Default Language'
FROM [Config].[Contract] a
join [dbo].[ServiceProvider] b on a.ServiceProviderID = b.ServiceProviderID
join [Config].[Languages] c on a.LanguageId = c.Id
我需要将每一行转换成详细的报告,如下所示:
从:
Name Program|Code|ACTIVE|Contract Start Date|Contract End Date |Description|Service provider’s name|Country |Default Language
收件人:
Name Program:
Code:
ACTIVE:
Contract Start Date:
Contract End Date:
Description:
Service provider’s name:
Country:
Default Language:
如何在SQL / T-SQL中将其转换?
答案 0 :(得分:1)
如果您希望将其作为单个字符串,只需将所有内容连接起来,并在其之间插入换行符?
SELECT
CONCAT(
'Name of the program : ', a.name, CHAR(13), CHAR(10),
'Code : ', a.ContractCode, CHAR(13), CHAR(10),
'Active : ', case a.IsActive when 1 THEN 'YES' when 0 then 'NO' end, CHAR(13), CHAR(10),
etc, etc
)
AS report_string
FROM [Config].[Contract] a
join [dbo].[ServiceProvider] b on a.ServiceProviderID = b.ServiceProviderID
join [Config].[Languages] c on a.LanguageId = c.Id