Link to home
Start Free TrialLog in
Avatar of Sheritlw
SheritlwFlag for United States of America

asked on

Error from union query

I am trying to list all schedules and then if there are services list those also, but I can't seem to get it to work.

I get the error...The query results cannot be enumerated more than once.

What is wrong with my Union?

Thanks



SELECT ISNULL(dbo.Schedules.ServiceID, 0) AS ServiceID, dbo.Schedules.SchedStartDateTime, dbo.Schedules.ServiceText, dbo.Schedules.CustomerID, 
                      ISNULL(dbo.Schedules.Completed, 0) AS Completed, dbo.Stylists.FullName, dbo.Stylists.StylistID, 'No Title' AS BeforePicName, 
                      '../images2020/resume-photo.jpg' AS BeforePicPath, 'No Title' AS AfterPicName, '../images2020/resume-photo.jpg' AS AfterPicPath
FROM         dbo.Schedules INNER JOIN
                      dbo.Stylists ON dbo.Schedules.UserID = dbo.Stylists.UserID

Where dbo.Schedules.CustomerID = 27
UNION 

SELECT ISNULL(dbo.Services.ServiceID, 0) AS ServiceID, dbo.Schedules.SchedStartDateTime, dbo.Schedules.ServiceText, dbo.Schedules.CustomerID, 
                      ISNULL(dbo.Services.Completed, 0) AS Completed, dbo.Stylists.FullName, dbo.Stylists.StylistID, ISNULL(dbo.Services.BeforePicName, 'No Title') AS BeforePicName, 
                      ISNULL(dbo.Services.BeforePicPath, '../images2020/resume-photo.jpg') AS BeforePicPath, ISNULL(dbo.Services.AfterPicName, 'No Title') AS AfterPicName, 
                      ISNULL(dbo.Services.AfterPicPath, '../images2020/resume-photo.jpg') AS AfterPicPath
FROM         dbo.Schedules INNER JOIN
                      dbo.Stylists ON dbo.Schedules.UserID = dbo.Stylists.UserID INNER JOIN
                      dbo.Services ON dbo.Schedules.ServiceID = dbo.Services.ServiceID

Where dbo.Schedules.CustomerID = 27
ORDER BY dbo.Schedules.SchedStartDateTime DESC

Open in new window

Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

Howdy....

My suspicion is the ORDER BY clause.  Change it to "ORDER BY 2" or "ORDER BY SchedStartDateTime"



Kent
Avatar of Sheritlw

ASKER

?? The order by shouldn't have anything to do with it. I have used Desc in multiple stored procedures without any problems.
Any other ideas?
Have you tried running the query in SSMS?

I think the problem is on LINQ rather than on the SQL.
Yes, It runs fine in ssms.  I also agree that it is in Linq, but what do I need to change to get it to work?

Thanks
SOLUTION
Avatar of Thomasian
Thomasian
Flag of Philippines image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Found and posted solution