473,569 Members | 2,542 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Wrong syntax in where smth in case ?

Hello all,

I belive, my problem is probably very easy to solve, but still, I
cannot find solution:

declare @i int
declare @z int
create table bubusilala (
[bubu] [int] NOT NULL ,
[gogo] [int] NOT NULL ,
[lala] [varchar] (3) NOT NULL )

insert into bubusilala (bubu,gogo,lala ) values (1,2,'ala')
insert into bubusilala (bubu,gogo,lala ) values (10,20,'aca')
insert into bubusilala (bubu,gogo,lala ) values (100,200,'bbb')
insert into bubusilala (bubu,gogo,lala ) values (11,21,'ccc')
insert into bubusilala (bubu,gogo,lala ) values (12,22,'abc')
insert into bubusilala (bubu,gogo,lala ) values (13,23,'cbd')

set @i = 10
set @z = 2

select * from bubusilala
where bubu in (
case when @i > @z then (1,2)
when @i < @z then (10,13) end)
and gogo like '%a%'

I get error, that statement is wrong in case near ','.
I supose, it is not possible, to get from case a group of values.
But why then, this works:
select * from bubusilala
where bubu in (
case when @i > @z then (1)
when @i < @z then (select gogo from bubusilala) end)
and gogo like '%a%'

This data are totaly simplified.

agrh ... any ideas??

Thank You in advance,

Mateusz

Jul 23 '05 #1
3 1499

"Matik" <ma****@sauron. xo.pl> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
Hello all,

I belive, my problem is probably very easy to solve, but still, I
cannot find solution:

declare @i int
declare @z int
create table bubusilala (
[bubu] [int] NOT NULL ,
[gogo] [int] NOT NULL ,
[lala] [varchar] (3) NOT NULL )

insert into bubusilala (bubu,gogo,lala ) values (1,2,'ala')
insert into bubusilala (bubu,gogo,lala ) values (10,20,'aca')
insert into bubusilala (bubu,gogo,lala ) values (100,200,'bbb')
insert into bubusilala (bubu,gogo,lala ) values (11,21,'ccc')
insert into bubusilala (bubu,gogo,lala ) values (12,22,'abc')
insert into bubusilala (bubu,gogo,lala ) values (13,23,'cbd')

set @i = 10
set @z = 2

select * from bubusilala
where bubu in (
case when @i > @z then (1,2)
when @i < @z then (10,13) end)
and gogo like '%a%'

I get error, that statement is wrong in case near ','.
I supose, it is not possible, to get from case a group of values.
But why then, this works:
select * from bubusilala
where bubu in (
case when @i > @z then (1)
when @i < @z then (select gogo from bubusilala) end)
and gogo like '%a%'

This data are totaly simplified.

agrh ... any ideas??

Thank You in advance,

Mateusz


CASE always returns a single value, so you can't use it in the way you want.
The easiest solution in a simple case is probably just to use IF ... THEN
...., but if your real query is more complicated or you have more possible
combinations of @i and @z, you might find an auxiliary table useful:

create table aux (
bubu int,
scenario int)

insert into aux (bubu, scenario) select 1, 1
insert into aux (bubu, scenario) select 2, 1
insert into aux (bubu, scenario) select 10, 2
insert into aux (bubu, scenario) select 13, 2

select *
from bubusilala b
join aux a
on a.bubu = b.bubu
where a.scenario = case when @i > @z then 1
when @i < @z then 2 end
and lala like '%a%'

You would probably need to modify this to work correctly, depending on what
the keys of your tables are, but this approach might be easier to maintain
than a series of IF... ELSE... blocks.

Simon
Jul 23 '05 #2
Simon,

Thank you very much for the answer, but unfortunately this does not
solve my problem.

The two parameter @i and @z can be various (this is a problem). But the
possible result in case, lets say you can imagin, is STATIC.

Let's say for two conditions:
Con1: @i < @z
Con2: @i > @z

possible values can be:
Con1: ONLY 1
Con2: ONLY 3 and 4

I need it, to make (corresponding to this two parameters) optimize
select statement, wher depending of this two values I will select once:
- all records, which have Collumn1 equal 1,
- all records, which have Collumn1 equal 3 or 4

I dont think that the solution with aux table, will help me in that
..... or maybe, if using like this:

select * from bubusilala
where bubu in (
case when @i > @z then (1)
when @i < @z then (select gogo from bubusilala) end)
and gogo like '%a%'

query will use all values which are in bubusilala table (or aux table)

Greatings

Mateusz

Jul 23 '05 #3
Matik (ma****@sauron. xo.pl) writes:
I supose, it is not possible, to get from case a group of values.
But why then, this works:
select * from bubusilala
where bubu in (
case when @i > @z then (1)
when @i < @z then (select gogo from bubusilala) end)
and gogo like '%a%'


(1) is a scalar value. The SELECT query will work if it returns only
one value, or if it is never invoked. Would it execute and return more
than one row, you will get an error. Since gogo is not like 'a%', the
subselect is never invoked.

This repro should give you something to work from:
create table bubusilala (
[bubu] [int] NOT NULL ,
[gogo] [int] NOT NULL ,
[lala] [varchar] (3) NOT NULL )

insert into bubusilala (bubu,gogo,lala ) values (1,2,'ala')
insert into bubusilala (bubu,gogo,lala ) values (10,20,'aca')
insert into bubusilala (bubu,gogo,lala ) values (100,200,'bbb')
insert into bubusilala (bubu,gogo,lala ) values (11,21,'ccc')
insert into bubusilala (bubu,gogo,lala ) values (12,22,'abc')
insert into bubusilala (bubu,gogo,lala ) values (13,23,'cbd')

declare @i integer, @z integer
set @i = 10
set @z = 2

select * from bubusilala
where case when @i > @z and bubu in (1, 2) OR
@i < @z and bubu in (10, 13)
then 1
else 0
end = 1
and lala like '%a%'
go
drop table bubusilala
--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

699
33517
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro capabilities, unfortunately. I'd like to know if it may be possible to add a powerful macro system to Python, while keeping its amazing syntax, and if it...
6
23507
by: Andy Wawa | last post by:
Hi, on a simple HTML (not an ASP!)-Site I try to connect to a sql server (MS): <html> <title>Test</title> <head> <script language="javascript"> <!-- Function showForm(){
8
3761
by: Penny | last post by:
Hi all, My browser throws this Select Case block back at me pointing out a syntax error on the line: 'Case < 251', between the word 'Case' and the '<' symbol. *************************** intWeightTotal = 550 Select Case intWeightTotal
2
15759
by: sunny076 | last post by:
Hi, I am confused with the syntax for NOT in MYSQL where clause and wonder if an expert in MYSQL can enlighten me. There are possibly two places NOT can go in: select * from employee_data where NOT employee_id LIKE 'A%' select * from employee_data where employee_id NOT LIKE 'A%'
4
6294
by: dharmadam | last post by:
update prescriber_cross_ref set preferred_in = 'Y' where prescriber_id = (with temp1 as (select A.prescriber_id,A.prescriber_dea_nb,A.prescriber_ama_nb,A.prescriber_aoa_nb from claimsa.tprscbr_prescribe1 A, prescriber_cross_ref B where A.prescriber_id = B.prescriber_id), with temp2 as (select case when prescriber_dea_nb <>'' and...
177
6897
by: C# Learner | last post by:
Why is C syntax so uneasy on the eye? In its day, was it _really_ designed by snobby programmers to scare away potential "n00bs"? If so, and after 50+ years of programming research, why are programming languages still being designed with C's syntax? These questions drive me insane. Every waking minute...
16
1748
by: danu | last post by:
I have a structure : typedef struct{ char magicNum; int width; int height; int maxGrey; int pixels; } ImageT;
3
1437
by: Rob R. Ainscough | last post by:
What sick saddistic person came up with the JavaScript language syntax? And was it the same person (or group of people) that came up with the HTML syntax?? OMG, has no one noticed this hopeless poor choice of language syntax? And why-o-why is it case sensitive? Is it just a carry over from light weight parsers? If we can't get out of...
5
10794
by: Ryan | last post by:
{"POINTID":77902,"MAPID":762,"LONG":-122.21654892,"LAT":"37.1834331019","CITY":"Boulder Creek","STATE":"CA","DIST":5745.4} I get an "invalid label" error... I'm kinda new to this. Thanks!
0
7700
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8125
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7676
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7974
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2114
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1221
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.