CREATE PROCEDURE sp_rpt_data
@xrefid integer,
@order1 integer,
@order2 integer,
@order3 integer
AS
select sid.number, sid.date_of_call, sid.time_of_call
from data sid
where sid.station_xref_id = convert(varchar(6),@xrefid)
order by @order1, @order2, @order3
I cannot create the above procedure as I get the standard 'Error 1008: The SELECT item identified by the ORDER BY number 1 contains a variable as part of the expression identifying a column position.'
I know you can't order by a variable so I have configured this to use CASE and WHEN statements so it works like this...
CREATE PROCEDURE sp_rpt_data
@xrefid integer,
@order1 integer,
@order2 integer,
@order3 integer
AS
select sid.number, sid.date_of_call, sid.time_of_call
from data sid
where sid.station_xref_id = convert(varchar(6),@xrefid)
order by
case when @order1 = 1 or @order2 = 1 or @order3 = 1
then sid.number else null end,
case when @order1 = 2 or @order2 = 2 or @order3 = 2
then sid.date_of_call else null end,
case when @order1 = 3 or @order2 = 3 or @order3 = 3
then sid.time_of_call else null end
end
If you can picture the software which calls this procedure. There are three text boxes for the number, date_of_call and time_of_call.
The way a user selects the sort order is by entering the numbers 1 to 3 which relates to the sort order. These are passed into the variables @order1, @order2 and @order3. I thought this all worked ok but have come across a big problem. If a user specifies (for example) @order1 = 3, @order2 = 2 and @order3 = 1 the sort order will not reflect this as my sort order of columns is pre-defined.
I am really stuck and an't find a solution. Ideally I want to be able to sort by numbers and not the column name but I don't know how I can specify this. If I try and use the variable I'm back to square one with the Error 1008.
Any help would be much appreciated.
@xrefid integer,
@order1 integer,
@order2 integer,
@order3 integer
AS
select sid.number, sid.date_of_call, sid.time_of_call
from data sid
where sid.station_xref_id = convert(varchar(6),@xrefid)
order by @order1, @order2, @order3
I cannot create the above procedure as I get the standard 'Error 1008: The SELECT item identified by the ORDER BY number 1 contains a variable as part of the expression identifying a column position.'
I know you can't order by a variable so I have configured this to use CASE and WHEN statements so it works like this...
CREATE PROCEDURE sp_rpt_data
@xrefid integer,
@order1 integer,
@order2 integer,
@order3 integer
AS
select sid.number, sid.date_of_call, sid.time_of_call
from data sid
where sid.station_xref_id = convert(varchar(6),@xrefid)
order by
case when @order1 = 1 or @order2 = 1 or @order3 = 1
then sid.number else null end,
case when @order1 = 2 or @order2 = 2 or @order3 = 2
then sid.date_of_call else null end,
case when @order1 = 3 or @order2 = 3 or @order3 = 3
then sid.time_of_call else null end
end
If you can picture the software which calls this procedure. There are three text boxes for the number, date_of_call and time_of_call.
The way a user selects the sort order is by entering the numbers 1 to 3 which relates to the sort order. These are passed into the variables @order1, @order2 and @order3. I thought this all worked ok but have come across a big problem. If a user specifies (for example) @order1 = 3, @order2 = 2 and @order3 = 1 the sort order will not reflect this as my sort order of columns is pre-defined.
I am really stuck and an't find a solution. Ideally I want to be able to sort by numbers and not the column name but I don't know how I can specify this. If I try and use the variable I'm back to square one with the Error 1008.
Any help would be much appreciated.