| View previous topic :: View next topic |
| Author |
Message |
Willy Verbiest Guest
|
Posted: Wed Feb 16, 2005 11:01 am Post subject: SQL ORDER BY as parameter |
|
|
I'm trying to sort a grid by clicking on the title bar but it doesnt work.
procedure TfrmOrdersSelection.DBGrid_OrdersTitleClick(Column: TColumn);
begin
with qryOrders do
begin
DisableControls;
Active := false;
Parameters.ParamByName('SortField').Value := Column.FieldName;
Active := true;
DBGrid_Orders.Columns.RestoreDefaults;
Column.Title.Font.Color := clBlue;
EnableControls;
end;
end;
The SQL for qryOrders is:
SELECT * from tblOrders ORDER BY :SortField
I have created a parameter in the qryOrder with the name 'SortField' and
a start value of 'OrderNo'
The query has no DataSource.
Thanks in advance,
Willy Verbiest
|
|
| Back to top |
|
 |
Bill Todd Guest
|
Posted: Wed Feb 16, 2005 12:38 pm Post subject: Re: SQL ORDER BY as parameter |
|
|
You cannot use parameters for database object names. They can only be used
for values.
--
Bill Todd (TeamB)
TeamB cannot answer questions received via email
|
|
| Back to top |
|
 |
Kim S Guest
|
Posted: Wed Feb 16, 2005 3:43 pm Post subject: Re: SQL ORDER BY as parameter |
|
|
Bill Todd wrote:
| Quote: | You cannot use parameters for database object names. They can only be
used for values.
Bill, Does this mean that it is not possible to use parameters in the |
SORT BY part of an SQL? (I'm having a similar problem and were going to
ask).
Regards, Kim
|
|
| Back to top |
|
 |
Del M Guest
|
Posted: Wed Feb 16, 2005 4:07 pm Post subject: Re: SQL ORDER BY as parameter |
|
|
You can use parameters to do sorting but you need to build a special sql
statement for it. What database ? In MS SQL you would have to do this
execute('select * from sometable order by ' + SortParameter)
|
|
| Back to top |
|
 |
Kim Stahnke Guest
|
Posted: Wed Feb 16, 2005 7:21 pm Post subject: Re: SQL ORDER BY as parameter |
|
|
I'm using an Access db for this. I've tried to use parameters like in the
WHERE part (:param1...) but it seems not to work at all - and it gives no
error indication either.
Kim
"Del M" <Del.Murray (AT) CreditHawk (DOT) Net> wrote
| Quote: | You can use parameters to do sorting but you need to build a special sql
statement for it. What database ? In MS SQL you would have to do this
execute('select * from sometable order by ' + SortParameter)
|
|
|
| Back to top |
|
 |
Bill Todd Guest
|
Posted: Wed Feb 16, 2005 10:43 pm Post subject: Re: SQL ORDER BY as parameter |
|
|
That is not a SQL parameter, but rather a variable. You can use variables for anything when you build the SQL statement dynamically.
--
Bill Todd (TeamB)
TeamB cannot answer questions received via email
Del M wrote:
| Quote: | You can use parameters to do sorting but you need to build a special sql
statement for it. What database ? In MS SQL you would have to do this
execute('select * from sometable order by ' + SortParameter)
|
|
|
| Back to top |
|
 |
Bill Todd Guest
|
Posted: Wed Feb 16, 2005 10:43 pm Post subject: Re: SQL ORDER BY as parameter |
|
|
That is correct. It is not possible. You can only use parameters for a value, not for the name of a database object such as a table or column.
--
Bill Todd (TeamB)
TeamB cannot answer questions received via email
Kim S wrote:
| Quote: | Does this mean that it is not possible to use parameters in the SORT BY part of an SQL?
|
|
|
| Back to top |
|
 |
Bill Todd Guest
|
Posted: Wed Feb 16, 2005 10:43 pm Post subject: Re: SQL ORDER BY as parameter |
|
|
Show us the actual SQL statement that does not work and explain in what way it does not work (get error message, no records returned when you think there should be, ...).
--
Bill Todd (TeamB)
TeamB cannot answer questions received via email
Kim Stahnke wrote:
| Quote: | I'm using an Access db for this. I've tried to use parameters like in the
WHERE part (:param1...) but it seems not to work at all - and it gives no
error indication either.
Kim
"Del M" <Del.Murray (AT) CreditHawk (DOT) Net> wrote in message
news:42136e12$1 (AT) newsgroups (DOT) borland.com...
You can use parameters to do sorting but you need to build a special sql
statement for it. What database ? In MS SQL you would have to do this
execute('select * from sometable order by ' + SortParameter)
|
|
|
| Back to top |
|
 |
Kim S Guest
|
Posted: Thu Feb 17, 2005 7:48 am Post subject: Re: SQL ORDER BY as parameter |
|
|
Bill Todd wrote:
| Quote: | Show us the actual SQL statement that does not work and explain in what way it does not work (get error message, no records returned when you think there should be, ...).
I think you already explained that it can not be done, but here is a |
sample sql:
SELECT a, b, c
FROM d
ORDER BY :p
It selects the records but does not order them other than by primary
key. It does not give any error messages either. The ORDER BY part is
simply ignored.
Regards, Kim
|
|
| Back to top |
|
 |
Bill Todd Guest
|
Posted: Thu Feb 17, 2005 2:00 pm Post subject: Re: SQL ORDER BY as parameter |
|
|
In your previous message you said, "I've tried to use parameters like in
the WHERE part (:param1...) but it seems not to work at all". I do not see
a parameter in the WHERE clause in the SELECT in your latest message so I
do not understand what your question is.
--
Bill Todd (TeamB)
TeamB cannot answer questions received via email
|
|
| Back to top |
|
 |
treok Guest
|
Posted: Wed Feb 23, 2005 10:26 am Post subject: Re: SQL ORDER BY as parameter |
|
|
There is a possibility:
declare @TheOrder int
set @TheOrder = :param
select * from Table order by case
when @TheOrder = 0 then Field1
when @TheOrder = 1 then Field2 end
--- posted by geoForum on http://delphi.newswhat.com
|
|
| Back to top |
|
 |
|