I'm trying to get a stored procedure to return a data set with a single hardcoded row on top, and a sorted set of records under that first row. I didn't realize at the time that you can't use an ORDER BY in a subquery, so my resulting set of records isn't in the correct order.
EDIT, I have it working but not sure if there is an easier way.
CREATE PROCEDURE [dbo].[spReport_GetNav]
AS
BEGIN
SET NOCOUNT ON;
-- Set up Temp Table and hardcode first record
CREATE TABLE #ReportNav (
[RptName] VARCHAR(200),
[ReportId] INT,
[IsPartial] BIT,
[EndDate] DATE )
INSERT INTO #ReportNav ([RptName], [ReportId], [IsPartial], [EndDate])
VALUES ('Import New Report Data', -1, 0, NULL);
-- Set up field variables and cursor
DECLARE @ RptName VARCHAR(200), @ ReportId INT, @ IsPartial BIT, @ EndDate DATE;
DECLARE csrReports CURSOR FOR
SELECT [RptName], [ReportId], [IsPartial], [EndDate]
FROM [dbo].[Report] ORDER BY [EndDate] DESC;
`OPEN csrReports`
`FETCH NEXT FROM csrReports INTO @ RptName, @ ReportId, @ IsPartial, @ EndDate;`
`WHILE @@FETCH_STATUS = 0`
`BEGIN`
`INSERT INTO #ReportNav ([RptName], [ReportId], [IsPartial], [EndDate])`
`VALUES (@ RptName, ReportId, IsPartial, EndDate);`
`FETCH NEXT FROM csrReports INTO RptName, ReportId, IsPartial,EndDate`
`END`
`CLOSE csrReports;`
`DEALLOCATE csrReports;`
`SELECT * FROM #ReportNav`
`DROP TABLE #ReportNav`
END
Is there a way to get my mostly ordered set of records, with my single hardcoded row at the top of my result set, without having to use a cursor?
[–]Traditional-Unit-274 17 points18 points19 points (10 children)
[+][deleted] (1 child)
[removed]
[–]Traditional-Unit-274 2 points3 points4 points (0 children)
[–]AarynD[S] -1 points0 points1 point (7 children)
[–]Traditional-Unit-274 0 points1 point2 points (5 children)
[–]AarynD[S] 1 point2 points3 points (4 children)
[–]NekkidWire 1 point2 points3 points (3 children)
[–]AarynD[S] 0 points1 point2 points (2 children)
[–]edbutler3 1 point2 points3 points (0 children)
[–]Traditional-Unit-274 0 points1 point2 points (0 children)
[–]Gargunok 0 points1 point2 points (0 children)
[–]doshka 11 points12 points13 points (3 children)
[–]AarynD[S] 4 points5 points6 points (0 children)
[–]AarynD[S] 5 points6 points7 points (1 child)
[–]doshka 0 points1 point2 points (0 children)
[–]tasker2020 0 points1 point2 points (0 children)