Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

SQL Server order by in procedure

I have the following query which works fine in SQL Server:

SELECT InvoiceID, SUM(UnitPrice)
FROM Sales.InvoiceLines
GROUP BY InvoiceID
ORDER BY InvoiceID

When I put it in a procedure the order by clause is not accepted:

CREATE OR ALTER PROCEDURE dbo.ReportsSales
AS
    SET NOCOUNT ON
    BEGIN (
        SELECT InvoiceID, SUM(UnitPrice)
        FROM Sales.InvoiceLines
        GROUP BY InvoiceID
        ORDER BY InvoiceID)
END;

EXEC dbo.ReportsSales;

Why is that and is there any recommended way of dealing with it?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You must not use round brackets around the SELECT in the procedure:

CREATE OR ALTER PROCEDURE dbo.ReportsSales
AS
BEGIN
    SET NOCOUNT ON;

    SELECT InvoiceID, SUM(UnitPrice)
    FROM Sales.InvoiceLines
    GROUP BY InvoiceID
    ORDER BY InvoiceID;
END;



EXEC dbo.ReportsSales;

Then you should be fine.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading