Home All Groups Group Topic Archive Search About

SQL Help - dynamically change where condition

Author
20 Jan 2006 3:15 AM
Mike
I am trying to modify the where condition of this query based on certain
values that will be passed to this query. I get this error

Incorrect syntax near '!'.


select emp_id,
emp_name
from employee
where emp_code = 'FIN'
and
case when ('889977' != '') then (emp_id != '889977') else (1=1) end



*889977 is a string that will be inserted into this query dynamically by the
application.

Author
20 Jan 2006 3:52 AM
Jonathan Chong
I am afraid you can only do syntax like this:

select emp_id,
emp_name
from employee
where emp_code = 'FIN'
and emp_id != case when ('889977' != '') then ( '889977') end

Show quote
"Mike" <M***@discussions.microsoft.com> wrote in message
news:C4AD144B-96C2-4E84-AB18-2B9B9443A357@microsoft.com...
> I am trying to modify the where condition of this query based on certain
> values that will be passed to this query. I get this error
>
> Incorrect syntax near '!'.
>
>
> select emp_id,
>  emp_name
> from employee
> where emp_code = 'FIN'
> and
> case when ('889977' != '') then (emp_id != '889977') else (1=1) end
>
>
>
> *889977 is a string that will be inserted into this query dynamically by
the
> application.
Author
20 Jan 2006 4:23 AM
Uri Dimant
Mike ,how about

if @var <>''
begin
     select emp_id,
     emp_name
     from employee
    where emp_code = 'FIN'
    and emp_id = @var
end


Show quote
"Mike" <M***@discussions.microsoft.com> wrote in message
news:C4AD144B-96C2-4E84-AB18-2B9B9443A357@microsoft.com...
>I am trying to modify the where condition of this query based on certain
> values that will be passed to this query. I get this error
>
> Incorrect syntax near '!'.
>
>
> select emp_id,
> emp_name
> from employee
> where emp_code = 'FIN'
> and
> case when ('889977' != '') then (emp_id != '889977') else (1=1) end
>
>
>
> *889977 is a string that will be inserted into this query dynamically by
> the
> application.
Author
20 Jan 2006 6:08 AM
--CELKO--
Pass the search string as a parameter to a procedure; AVOID dynamic
SQL!!

SELECT  emp_id,  emp_name
FROM Personnel -- use class or collective name for a table
WHERE  emp_code = 'FIN'
   AND @my_emp_id = COALESCE (@my_emp_id, emp_id);

Pass a NULL when you want all of the employees.  Never use an empty
string for a missing value.  It needs to a NULL or an explicit missing
value code.

Also that "!=" is not SQL; we use "<>" in Standard SQL -- that makes
you look like a BASIC, C, C++, VB programmer.  Yuch!
Author
20 Jan 2006 7:38 AM
Tony Rogerson
> SELECT  emp_id,  emp_name
> FROM Personnel -- use class or collective name for a table
> WHERE  emp_code = 'FIN'
>   AND @my_emp_id = COALESCE (@my_emp_id, emp_id);

Now consider this query, emp_id could well be a unique value, whereas
emp_code, there might only be 5 emp_codes.

The plan the above query will get is terrible, even if you had an index on
emp_code it will still take a good deal of processing.

If you wrote it using IF ELSE you could get a good plan...

IF @my_emp_id IS NULL
    SELECT  emp_id,  emp_name
    FROM Personnel -- use class or collective name for a table
    WHERE  emp_code = 'FIN'

ELSE
    SELECT  emp_id,  emp_name
    FROM Personnel -- use class or collective name for a table
    WHERE  emp_code = 'FIN'
         AND emp_id = @my_emp_id


If you pass @my_emp_id then, assuming an index on emp_id you would get an
index seek and a very quick and performant query.

I've seen from other posts you would not use IF ELSE, that seems
inconsistent with your book; but just how would you get round this problem?
Would you code multiple stored procedures? In this case there are only two
branches, but what if there where 10 optional parameters - that is a lot of
stored procedures all because you won't use IF ELSE.

--
Tony Rogerson
SQL Server MVP
http://sqlserverfaq.com - free video tutorials


Show quote
"--CELKO--" <jcelko***@earthlink.net> wrote in message
news:1137737287.697850.159150@z14g2000cwz.googlegroups.com...
> Pass the search string as a parameter to a procedure; AVOID dynamic
> SQL!!
>
> SELECT  emp_id,  emp_name
> FROM Personnel -- use class or collective name for a table
> WHERE  emp_code = 'FIN'
>   AND @my_emp_id = COALESCE (@my_emp_id, emp_id);
>
> Pass a NULL when you want all of the employees.  Never use an empty
> string for a missing value.  It needs to a NULL or an explicit missing
> value code.
>
> Also that "!=" is not SQL; we use "<>" in Standard SQL -- that makes
> you look like a BASIC, C, C++, VB programmer.  Yuch!
>
Author
20 Jan 2006 1:55 PM
Mike
Thanks Tony,
I am now using IF - ELSE. this solved my problem.

Show quote
"Tony Rogerson" wrote:

> > SELECT  emp_id,  emp_name
> > FROM Personnel -- use class or collective name for a table
> > WHERE  emp_code = 'FIN'
> >   AND @my_emp_id = COALESCE (@my_emp_id, emp_id);
>
> Now consider this query, emp_id could well be a unique value, whereas
> emp_code, there might only be 5 emp_codes.
>
> The plan the above query will get is terrible, even if you had an index on
> emp_code it will still take a good deal of processing.
>
> If you wrote it using IF ELSE you could get a good plan...
>
> IF @my_emp_id IS NULL
>     SELECT  emp_id,  emp_name
>     FROM Personnel -- use class or collective name for a table
>     WHERE  emp_code = 'FIN'
>
> ELSE
>     SELECT  emp_id,  emp_name
>     FROM Personnel -- use class or collective name for a table
>     WHERE  emp_code = 'FIN'
>          AND emp_id = @my_emp_id
>
>
> If you pass @my_emp_id then, assuming an index on emp_id you would get an
> index seek and a very quick and performant query.
>
> I've seen from other posts you would not use IF ELSE, that seems
> inconsistent with your book; but just how would you get round this problem?
> Would you code multiple stored procedures? In this case there are only two
> branches, but what if there where 10 optional parameters - that is a lot of
> stored procedures all because you won't use IF ELSE.
>
> --
> Tony Rogerson
> SQL Server MVP
> http://sqlserverfaq.com - free video tutorials
>
>
> "--CELKO--" <jcelko***@earthlink.net> wrote in message
> news:1137737287.697850.159150@z14g2000cwz.googlegroups.com...
> > Pass the search string as a parameter to a procedure; AVOID dynamic
> > SQL!!
> >
> > SELECT  emp_id,  emp_name
> > FROM Personnel -- use class or collective name for a table
> > WHERE  emp_code = 'FIN'
> >   AND @my_emp_id = COALESCE (@my_emp_id, emp_id);
> >
> > Pass a NULL when you want all of the employees.  Never use an empty
> > string for a missing value.  It needs to a NULL or an explicit missing
> > value code.
> >
> > Also that "!=" is not SQL; we use "<>" in Standard SQL -- that makes
> > you look like a BASIC, C, C++, VB programmer.  Yuch!
> >
>
>
>
Author
20 Jan 2006 7:40 AM
Tony Rogerson
if coalesce( @emp_id, '' ) = ''
    select emp_id,
     emp_name
    from employee
    where emp_code = 'FIN'
else
    select emp_id,
     emp_name
    from employee
    where emp_code = 'FIN'
    and emp_id <> @emp_id


--
Tony Rogerson
SQL Server MVP
http://sqlserverfaq.com - free video tutorials


Show quote
"Mike" <M***@discussions.microsoft.com> wrote in message
news:C4AD144B-96C2-4E84-AB18-2B9B9443A357@microsoft.com...
>I am trying to modify the where condition of this query based on certain
> values that will be passed to this query. I get this error
>
> Incorrect syntax near '!'.
>
>
> select emp_id,
> emp_name
> from employee
> where emp_code = 'FIN'
> and
> case when ('889977' != '') then (emp_id != '889977') else (1=1) end
>
>
>
> *889977 is a string that will be inserted into this query dynamically by
> the
> application.

AddThis Social Bookmark Button