I'm trying to get data from multiple tables and put them in a temporary table, and sort them.Here's my query:@SortDirection intASDECLARE @Users TABLE( UserID int, Points int)IF @SortDirection = 0BEGIN INSERT INTO @Users (UserID, Points) ( SELECT U.UserID, ( SELECT SUM(Score) FROM tblPoints P WHERE P.UserID = U.UserID ) AS Points FROM tblUsers U ORDER BY Points ASC -- error here )ENDELSEBEGIN INSERT INTO @Users (UserID, Points) ( SELECT U.UserID, ( SELECT SUM(Score) FROM tblPoints P WHERE P.UserID = U.UserID ) AS Points FROM tblUsers U ORDER BY Points DESC -- error here )ENDSELECT *FROM @Users
When I try to run my stored procedure, I get two errors saying:"Incorrect syntax near the keyword 'ORDER'."I've put a comment where these errors occur. Does anyone know how to fix this?