I have built a Stored Procedure that takes input parameters and returns a result set so that I can consume it for Reporting Services. However, the user wanted to choose the ORDER BY columns of their choice. So, I tried something like this:
CREATE PROCEDURE [dbo].[usp_RevenueReport]
(@ProductNumber INT,
@ProductName VARCHAR(MAX),
@ProductType VARCHAR(MAX), @SortColumns VARCHAR(MAX))
SELECT [ProductNumber],
[ProductName],
[ProductType]
FROM [dbo].[Products]
WHERE [ProductName] IN (SELECT value FROM STRING_SPLIT(@ProductName, ',') WHERE RTRIM(value) <> '')
AND [ProductType] IN (SELECT value FROM STRING_SPLIT(@ProductType, ',') WHERE RTRIM(value) <> '')
ORDER BY (SELECT value FROM STRING_SPLIT(@SortColumns, ',') WHERE RTRIM(value) <> '');
It is throwing an error when I pass multiple columns for sorting. How can I sort based on what I pass?
Msg 512, Level 16, State 1, Line 1 Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
Here is my environment: Microsoft Azure SQL Data Warehouse - 10.0.10887.0 Jan 23 2020 07:36:54 Copyright (c) Microsoft Corporation