Альтернатива скалярной функции в MS SQL - PullRequest
0 голосов
/ 11 мая 2018

Вместо запроса ниже, что я могу использовать для преобразования данных sql в объект json.

CREATE FUNCTION [dbo].[ES_GetSchools](@IdListing NVARCHAR(400))
 RETURNS NVARCHAR(MAX)  
 AS
 BEGIN
 RETURN(
    SELECT   
            CASE WHEN ISNULL(SchoolDistrictId,'')<>'' THEN 
            SUBSTRING(SchoolDistrictId,1,CHARINDEX(':',SchoolDistrictId)-1)ELSE NULL END AS 'district.name',
            CASE WHEN ISNULL(SchoolDistrictId,'')<>'' THEN 
            SUBSTRING(SchoolDistrictId,CHARINDEX(':',SchoolDistrictId)+1,LEN(SchoolDistrictId))ELSE NULL END AS 'district.id',
            CASE WHEN ISNULL(SchoolDistrictSEOPath,'')<>'' AND SchoolDistrictSEOPath<>'0'  THEN 
            SchoolDistrictSEOPath ELSE NULL end 'district.seo',
            CASE WHEN ISNULL(SchoolElementary,'')<>'' THEN 
            CASE WHEN CHARINDEX(':',SchoolElementary)>0 THEN
            SUBSTRING(SchoolElementary,1,CHARINDEX(':',SchoolElementary)-1) ELSE SchoolElementary END ELSE NULL END AS 'elementary.name',
            CASE WHEN ISNULL(SchoolElementary,'')<>'' THEN 
            CASE WHEN CHARINDEX(':',SchoolElementary)>0 THEN
            SUBSTRING(SchoolElementary,CHARINDEX(':',SchoolElementary)+1,LEN(SchoolElementary)) ELSE NULL END ELSE NULL END AS 'elementary.id',
            CASE WHEN ISNULL(SchoolHigh,'')<>'' THEN 
            CASE WHEN CHARINDEX(':',SchoolHigh)>0 THEN
            SUBSTRING(SchoolHigh,1,CHARINDEX(':',SchoolHigh)-1)ELSE SchoolHigh END ELSE NULL END AS 'high.name',
            CASE WHEN ISNULL(SchoolHigh,'')<>'' THEN 
            CASE WHEN CHARINDEX(':',SchoolHigh)>0 THEN
            SUBSTRING(SchoolHigh,CHARINDEX(':',SchoolHigh)+1,len(SchoolHigh))ELSE NULL END ELSE NULL END AS 'high.id',
            CASE WHEN ISNULL(SchoolMiddle,'')<>'' THEN 
            CASE WHEN CHARINDEX(':',SchoolMiddle)>0 THEN
            SUBSTRING(SchoolMiddle,1,CHARINDEX(':',SchoolMiddle)-1)ELSE SchoolMiddle END  ELSE NULL END AS 'middle.name',
            CASE WHEN ISNULL(SchoolMiddle,'')<>'' THEN 
            CASE WHEN CHARINDEX(':',SchoolMiddle)>0 THEN
            SUBSTRING(SchoolMiddle,CHARINDEX(':',SchoolMiddle)+1,len(SchoolMiddle))ELSE NULL END ELSE NULL END AS 'middle.id'
    FROM  Table_Name1 a WITH (NOLOCK)
          WHERE  a.IdListing = @IdListing 
FOR JSON PATH,WITHOUT_ARRAY_WRAPPER
)
END

GO

SELECT dbo.[ES_GetSchools](Idlisting) from Table_Name1 a join Table_Name2 b on a.idlisting=b.idlisting 

1 Ответ

0 голосов
/ 12 мая 2018

Используйте следующий синтаксис

SELECT field1, field2
FROM table
FOR JSON AUTO 
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...