I recently coded a stored procedure on SQL Server 2019 (14.0.3192.2) in AWS that utilized the function STRING_AGG with no issues. When I moved the code to my local test db it complained about that method.

Upon research I proceeded to update my local sql server db service from 14.x version to the latest 15.0.2 and tried to install the sproc again; to the failure

Incorrect syntax near '"'',STRING_AGG(ActionDATE, ''","''),''"'.

This feature has been viable since SQL Server 2017...so being missing in the local version of 2019 is somewhat puzzling.

Question

What recourse do I have besides installing SQL Server 2019 Developer edition to resolve this issue?


Intention

Note that the failure line with STRING_AGG is trying to create a JSON sub array of dates such as

[ '01/24/2020', '01/28/2020' ]:

Code

CREATE PROCEDURE [hcwfba].[GetOperationsAndActionDates] 
    @operationId int,
    @locationId int  
AS       

WITH  [DATA] AS (SELECT DISTINCT id_Operation, id_Location , CONVERT(varchar, ActionDATE ,101) AS [ActionDATE] FROM OperationDetail)

SELECT distinct 
         PD.id_Operation
       , PD.id_Location
       , JSON_QUERY((SELECT CONCAT(''["'',STRING_AGG(ActionDATE, ''","''),''"]'') FROM [DATA] DT2 WHERE DT2.id_Location = PD.id_Location)) As Dates
FROM [DATA] PD
WHERE PD.id_Operation = @operationId 
AND PD.id_Location = @locationId
FOR JSON AUTO;

RETURN @@ROWCOUNT 

JSON Result

[
    {
        "id_Operation": 13,
        "id_Location": 8675309,
        "Dates": [
            "12/03/2018",
            "08/27/2018",
            "10/22/2018"
        ]
    }
]
有帮助吗?

解决方案

Per my comments: Have you tried to use the STRING_AGG() function with a very simple test case to confirm it's an issue with the function and not your syntax in your main query?

It seems your syntax was not formatted properly, rather than the STRING_AGG() function not being available on your instance version.

许可以下: CC-BY-SA归因
不隶属于 dba.stackexchange
scroll top