Hello all,
Situation:
I am setting up a stored procedure for a report to be printed on the webpage. I want to allow any field to be sorted ascending or descending when they click on the table column header name. To speed everything up, I copy the recordset into a multi-dimentional array, and then close the recordset and db connections.
The problem:
Because I am using the array, I cannot retrieve the field name into the array. When I create the table column header names to be links, I want to specify a querystring to be attached to it to specify that it is that column that should be the order by field. Since I cannot pull the name of the field from the recordset (because I am using arrays), I created a static function with the field names. So now the problem is that SQL Server will not let me create a variable to intake the value for the order by statement. Here is what I have for code...
But when I try to create this so that sortOrder will be the parameter for the order by statement, SQL Server generates the following error:
Error 1008: The SELECT item identified by the ORDER BY number 1 contains a variable as part of the expression identifying a column position. Variables are only allowed when ordering by an expression referencing a column name.The SELECT item identified by the ORDER BY number 1 contains a variable as part of the expression identifying a column position. Variables are only allowed when ordering by an expression referencing a column name.
Does anyone know why this is happening, and how I can make it do what I'm trying to do?
-Ovatvvon :-Q
Situation:
I am setting up a stored procedure for a report to be printed on the webpage. I want to allow any field to be sorted ascending or descending when they click on the table column header name. To speed everything up, I copy the recordset into a multi-dimentional array, and then close the recordset and db connections.
The problem:
Because I am using the array, I cannot retrieve the field name into the array. When I create the table column header names to be links, I want to specify a querystring to be attached to it to specify that it is that column that should be the order by field. Since I cannot pull the name of the field from the recordset (because I am using arrays), I created a static function with the field names. So now the problem is that SQL Server will not let me create a variable to intake the value for the order by statement. Here is what I have for code...
Code:
CREATE PROCEDURE [dbo].[sql_searchClientAccount]
@company varchar(10),
@id varchar(7),
@name varchar(50),
@status varchar(1),
@phone varchar(16),
@sortOrder int
AS
IF @status != '0'
SELECT DISTINCT
Top 501 CLIENT.id, CLIENT.dba_name, CLIENT.phone, CLIENT.fax, CLIENT.payroll_rep, CP_CLIENT.csr, CP_CLIENT.cam,
COUNT(DISTINCT EMPLOYEE_CLIENT.employee_id) AS employeeCount
FROM CLIENT LEFT OUTER JOIN
CP_CLIENT ON CLIENT.id = CP_CLIENT.client_id LEFT OUTER JOIN
EMPLOYEE_CLIENT ON CLIENT.id = EMPLOYEE_CLIENT.client_id
WHERE CLIENT.id LIKE @id AND CLIENT.company LIKE @company AND CLIENT.dba_name LIKE @name AND CLIENT.phone LIKE @phone AND CLIENT.client_status = @status
GROUP BY CLIENT.id, CLIENT.dba_name, CLIENT.phone, CLIENT.fax, CLIENT.payroll_rep, CP_CLIENT.csr, CP_CLIENT.cam
ORDER BY @sortOrder
ELSE
SELECT DISTINCT
Top 501 CLIENT.id, CLIENT.dba_name, CLIENT.phone, CLIENT.fax, CLIENT.payroll_rep, CP_CLIENT.csr, CP_CLIENT.cam,
COUNT(DISTINCT EMPLOYEE_CLIENT.employee_id) AS employeeCount
FROM CLIENT LEFT OUTER JOIN
CP_CLIENT ON CLIENT.id = CP_CLIENT.client_id LEFT OUTER JOIN
EMPLOYEE_CLIENT ON CLIENT.id = EMPLOYEE_CLIENT.client_id
WHERE CLIENT.id LIKE @id AND CLIENT.company LIKE @company AND CLIENT.dba_name LIKE @name AND (CLIENT.client_status='A' OR CLIENT.client_status='P') AND CLIENT.phone LIKE @phone
GROUP BY CLIENT.id, CLIENT.dba_name, CLIENT.phone, CLIENT.fax, CLIENT.payroll_rep, CP_CLIENT.csr, CP_CLIENT.cam
ORDER BY @sortOrder
GO
But when I try to create this so that sortOrder will be the parameter for the order by statement, SQL Server generates the following error:
Error 1008: The SELECT item identified by the ORDER BY number 1 contains a variable as part of the expression identifying a column position. Variables are only allowed when ordering by an expression referencing a column name.The SELECT item identified by the ORDER BY number 1 contains a variable as part of the expression identifying a column position. Variables are only allowed when ordering by an expression referencing a column name.
Does anyone know why this is happening, and how I can make it do what I'm trying to do?
-Ovatvvon :-Q