Home All Groups Group Topic Archive Search About

SQL code passed with a parameter

Author
1 Oct 2005 9:46 PM
Howard
Hi everybody

My application calls a stored procedure that runs a select statement. The
procedure accepts an input parameter (varchar) named @OrderBy. User chooses a
sort order from a combo-box and the application supplies the order by clause
to the stored procedure with the @OrderBy parameter.

For example, the value of @OrderBy could be ' ORDER BY ColumnA, ColumnB,
ColumnC DESC'.

My question is how to use the value of the @OrderBy parameter inside my
SELECT statement, as it obviously doesn't work like this:
SELECT ... FROM ... WHERE ... ORDER BY @OrderBy

Thanks
Howard

Author
1 Oct 2005 9:52 PM
Mike Epprecht (SQL MVP)
Hi

http://www.aspfaq.com/show.asp?id=2501
and
http://www.sommarskog.se/dynamic_sql.html

Regards
--------------------------------
Mike Epprecht, Microsoft SQL Server MVP
Zurich, Switzerland

IM: m***@epprecht.net

MVP Program: http://www.microsoft.com/mvp

Blog: http://www.msmvps.com/epprecht/

Show quote
"Howard" <How***@discussions.microsoft.com> wrote in message
news:08F8FA2D-C32B-47BE-B0EA-231871E64333@microsoft.com...
> Hi everybody
>
> My application calls a stored procedure that runs a select statement. The
> procedure accepts an input parameter (varchar) named @OrderBy. User
> chooses a
> sort order from a combo-box and the application supplies the order by
> clause
> to the stored procedure with the @OrderBy parameter.
>
> For example, the value of @OrderBy could be ' ORDER BY ColumnA, ColumnB,
> ColumnC DESC'.
>
> My question is how to use the value of the @OrderBy parameter inside my
> SELECT statement, as it obviously doesn't work like this:
> SELECT ... FROM ... WHERE ... ORDER BY @OrderBy
>
> Thanks
> Howard
>
>

AddThis Social Bookmark Button