hi all, hope someone can help....
i'm having trouble calling an SP where the ORDER BY operator is specified as
a parameter when the SP is called
my SP is.....
CREATE PROCEDURE dbo.sp_CustSearch (@SearchFor VARCHAR(80) , @SortOrder
VARCHAR(50))
AS
BEGIN
SELECT first_name, last_name, postcode , address, town
FROM customer WITH (nolock)
WHERE (
UPPER (last_name) LIKE '%' + @SearchFor + '%'
OR UPPER(address.postcode) = @SearchFor )
ORDER BY @SortOrder
END
GO
the line causing the problem is ORDER BY @SortOrder
the error i get on checking the syntax is:
"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"
anyone know how to solve this? i'm guessing it's something simple.
enclosing @SortOrder in single quotes gives, unsuprisinlgy, unsorted
results.
trying ORDER BY '[' + @SortOrder + ']' gives the same error as
above
the only way i've managed to get it working so far is to dynamically build
the SQL statement and then execute that at the end. it's a little messy
which was why i was trying to get the above working.
thanks in advance. 4 8237
ok, found the answer....
use a CASE statement on the ORDER BY.....
ORDER BY
CASE @SortOrder
WHEN 'name' THEN upper(last_name)
WHEN 'postcode' THEN address.postcode
WHEN 'address' THEN address
END
"dave" <us****@polo.devilgas.com> wrote in message
news:%L****************@newsfe2-win.ntli.net... hi all, hope someone can help....
i'm having trouble calling an SP where the ORDER BY operator is specified
as a parameter when the SP is called
my SP is.....
CREATE PROCEDURE dbo.sp_CustSearch (@SearchFor VARCHAR(80) , @SortOrder VARCHAR(50)) AS
BEGIN
SELECT first_name, last_name, postcode , address, town FROM customer WITH (nolock) WHERE ( UPPER (last_name) LIKE '%' + @SearchFor + '%' OR UPPER(address.postcode) = @SearchFor ) ORDER BY @SortOrder
END GO
the line causing the problem is ORDER BY @SortOrder
the error i get on checking the syntax is: "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"
anyone know how to solve this? i'm guessing it's something simple.
enclosing @SortOrder in single quotes gives, unsuprisinlgy, unsorted results. trying ORDER BY '[' + @SortOrder + ']' gives the same error as above
the only way i've managed to get it working so far is to dynamically build the SQL statement and then execute that at the end. it's a little messy which was why i was trying to get the above working.
thanks in advance.
this approach can result in poor performance. just think: even if there
are 2 indexes on postcode and another one on address, the optimizer
cannot use neither index to get ordered results, because it must come
up with one generic plan, and it does not know at compile time how to
order the results.
this might perform better:
if @SortOrder = 'postcode'
then begin
select ...
order by address.postcode
end
else
begin
select ...
order by address
end
Even if don't see any difference when you run it agaist a small test
data set with no indexes, the difference in production could be dramatic
"AK" <st***********@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com... this approach can result in poor performance. just think: even if there are 2 indexes on postcode and another one on address, the optimizer cannot use neither index to get ordered results, because it must come up with one generic plan, and it does not know at compile time how to order the results.
this might perform better:
if @SortOrder = 'postcode' then begin select ... order by address.postcode end else begin select ... order by address end
Even if don't see any difference when you run it agaist a small test data set with no indexes, the difference in production could be dramatic
thanks steven (?). makes sense so i'll look at writing that approach into
all of the SP's where i use a non-static ORDER BY.
You welcome.
In fact, I'm Alexander. Yesterday I let my son Steven to look up his
e-mail at gmail.com on my PC. Now part of his e-mail is displayed in
Google Groups. How comes This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Dave |
last post by:
1) I know that we can define an external proc to be Fenced or NotFenced on
"CREATE PROCEDURE" command.
I don't see the FENCED / NOT FENCED option on "Create Procedure" for SQL
stored procs.
Is...
|
by: Jim Andersen |
last post by:
Just to let you know, and to help any future sorry sods who gets trapped in
the same black hole......
You can't just copy/move a working sql-statement into a stored procedure.
Working with a...
|
by: kentk |
last post by:
Is there a difference in how SQL Server 7 and SQL 2000 processes SQL passed
from a program by an ADO command object. Reason I ask is I rewrote a couple
applications a couple years ago were the SQL...
|
by: limsy |
last post by:
Hi ppl,
Sorry for asking such a NEWBIE question. I tried looking for answers but
cant find. Maybe its too easy. :(
I'm used to manual code ADO rather than this .NET wizard and stuff... and i
am...
|
by: sreekar |
last post by:
hi all,
I have some problem converting the stored procs from MS SQL2005 to MySQL. near the "like" keyword..................
CREATE PROCEDURE `xxx`
(
Name varchar(100),
FieldName...
|
by: Burt |
last post by:
I'm a stored proc guy, but a lot of people at my company use inline
sql in their apps, sometimes putting the sql in a text file, sometimes
hardcoding it. They don't see much benefit from procs, and...
|
by: Yash |
last post by:
Hi,
We are in the process of tuning the performance of our stored procs in
SQL 2000 and are looking for a tool that would help us in comparing
the result sets of an old SP and a modified SP. The...
|
by: Frank Calahan |
last post by:
I've been looking at LINQ and it seems very nice to be able to make
queries in code, but I use stored procs for efficiency. If LINQ to SQL
only works with SQL Server and stored procs are more...
|
by: Bogdan |
last post by:
Hi,
I have a stored procedure that uses JOINs to return columns from multiple
tables. I also have another stored proc that that takes a series of params
and updates multiple tables. I used the...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |