Re: Need to make Sort ORDER in SP defined by Input variable

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Larry Woods (larry_at_NOSPAMlwoods.com)
Date: 06/26/04


Date: Sat, 26 Jun 2004 09:55:14 -0700

Help.... I'm back!

I get an error when I try to store my SP using sp_executesql @SQL. Says
"Incorrect syntax near sp_executesql". So, I changed it to EXEC @SQL. Now,
when I execute the SP it gives me "Invalid column name 'SELECT ....'" where
it displays the complete SELECT statement.

HELP!

TIA,

Larry Woods

"Pablo" <Pablo@discussions.microsoft.com> wrote in message
news:462D3E08-EDFB-46CF-9F46-6452539FBCAC@microsoft.com...
> Hi,
> You can't do this by variable like
> ORDER BY @MyColumnName
>
> You should make a sql string and execute it by
> sp_executesql. Something like this
>
> SET @SQL='select * from Table Order by ' + @MyColumnName
> sp_executesql @SQL
>
> Regards,
> Pablo
>
>
> "Larry Woods" wrote:
>
> > I am trying to define my ORDER field and direction (Asc/Desc) as a
variable
> > (or variables) that I pass into a SP. SQL Server doesn't like this.
> >
> > How do I do it?
> >
> > TIA,
> >
> > Larry Woods
> >
> >
> >



Relevant Pages