Re: Using ORDER BY in a stored procedure by Rick
Rick
Sun Nov 02 18:53:28 CST 2003
"Bob Barrows" <reb01501@NOyahoo.SPAMcom> wrote in message
news:eGo77bVoDHA.1656@tk2msftngp13.phx.gbl...
> Rick Snagglehimer wrote:
> > I keep getting an error from the following SP snippet
> >
> > "Line 11: Incorrect syntax near 'BY'."
> >
> > It runs fine without the ORDER BY clause.
> >
> >>>>>>>>>>>>>>>>
> > [' + @user + ']
> > WHERE
> > [' + @cal + '].clientID = [' + @user1 + '].userID
> > AND [' + @cal + '].newID = ' + @userID + ' ORDER BY [' + @cal +
> > '].myAppointment'
> > EXEC (@sql)
> > GO
> >>>>>>>>>>>>>>>>
> >
> >
> >
> >
> > Any suggestions?
>
> Put a "Print @sql" statement in there to verify that the statement
contained
> in @sql is correct.
>
> Bob Barrows
>
> --
> Microsoft MVP - ASP/ASP.NET
> Please reply to the newsgroup. This email account is my spam trap so I
> don't check it very often. If you must reply off-line, then remove the
> "NO SPAM"
>
>
Looks OK to me. Not sure what I'm missing here-
SELECT [wcdemo].ID,
[wcdemousers].one,
[wcdemousers].two,
[wcdemo].appointmentStart,
[wcdemo].appointmentEnd,
[wcdemo].ownerID
FROM [wcdemo],
[wcdemousers]
WHERE
[wcdemo].clientID = [wcdemousers].userID
AND [wcdemo].ownerID = 1 ORDER BY
Server: Msg 170, Level 15, State 1, Line 11
Line 11: Incorrect syntax near 'BY'.
Stored Procedure: intra-fusedb.dbo.sp_CalendarNewAppointments
Return Code = 0
The actual statement-
CREATE PROCEDURE sp_CalendarNewAppointments
@userID varChar(255),
@users varChar(255),
@calendar varChar(255)
AS
DECLARE @sql varChar(255)
SELECT @sql = 'SELECT [' + @calendar +'].ID,
[' + @users + '].one,
[' + @users + '].two,
[' + @calendar + '].appointmentStart,
[' + @calendar + '].appointmentEnd,
[' + @calendar + '].ownerID
FROM [' + @calendar + '],
[' + @users + ']
WHERE
[' + @calendar + '].clientID = [' + @users + '].userID
AND [' + @calendar + '].ownerID = ' + @userID +
' ORDER BY [' + @calendar + '].appointmentStart'
Print @sql
EXEC (@sql)
GO