473,668 Members | 2,355 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

case-when error

hello i am learning how to write stored procedures in sql server, i
would like to know what's wrong with the following statement? please
help

the management studio gives me the following error:
Msg 156, Level 15, State 1, Procedure spDealMasterSea rch, Line 19
Incorrect syntax near the keyword 'CASE'.

USE [mydatabase]
GO
/****** Object: StoredProcedure [dbo].[spHouseUpdate] Script Date:
04/15/2008 16:02:50 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFI ER ON
GO
CREATE PROCEDURE [dbo].[spDealMasterSea rch]

@in_Location varchar(50),
@in_HouseType varchar(50),
@in_Size varchar(50),
@in_ExpectedPri ce varchar(50)

AS
BEGIN

SELECT dealMasterId From
dealmaster INNER JOIN houses
ON dealmaster.Hous eId = houses.HouseId
WHERE
houses.Location = @in_Location AND
houses.HouseTyp e = @in_HouseType AND
houses.Size

CASE
WHEN @in_Size = 'within1000' THEN <= 1000
WHEN @in_Size = 'more1000' THEN 1000
ELSE 0
END AND

houses.expected Price
CASE
WHEN @in_ExpectedPri ce = 'within5m' THEN <= 5
WHEN @in_ExpectedPri ce = 'within10m' THEN <=10
WHEN @in_ExpectedPri ce = 'dontcare' THEN 0
ELSE 0
END AND

houses.EntitySt atus = 'A' AND
dealmaster.Enti tyStatus = 'A' AND
dealmaster.expe ctedPrice <= 5 AND
dealmaster.stat us = 'offering';
END
Jun 27 '08 #1
2 1696
On Tue, 15 Apr 2008 02:27:31 -0700 (PDT), philip <ma******@gmail .com>
wrote:

CASE cannot be used in a WHERE clause.
-Tom.

>hello i am learning how to write stored procedures in sql server, i
would like to know what's wrong with the following statement? please
help

the management studio gives me the following error:
Msg 156, Level 15, State 1, Procedure spDealMasterSea rch, Line 19
Incorrect syntax near the keyword 'CASE'.

USE [mydatabase]
GO
/****** Object: StoredProcedure [dbo].[spHouseUpdate] Script Date:
04/15/2008 16:02:50 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFI ER ON
GO
CREATE PROCEDURE [dbo].[spDealMasterSea rch]

@in_Location varchar(50),
@in_HouseTyp e varchar(50),
@in_Size varchar(50),
@in_ExpectedPr ice varchar(50)

AS
BEGIN

SELECT dealMasterId From
dealmaster INNER JOIN houses
ON dealmaster.Hous eId = houses.HouseId
WHERE
houses.Location = @in_Location AND
houses.HouseTyp e = @in_HouseType AND
houses.Size

CASE
WHEN @in_Size = 'within1000' THEN <= 1000
WHEN @in_Size = 'more1000' THEN 1000
ELSE 0
END AND

houses.expected Price
CASE
WHEN @in_ExpectedPri ce = 'within5m' THEN <= 5
WHEN @in_ExpectedPri ce = 'within10m' THEN <=10
WHEN @in_ExpectedPri ce = 'dontcare' THEN 0
ELSE 0
END AND

houses.EntitySt atus = 'A' AND
dealmaster.Enti tyStatus = 'A' AND
dealmaster.expe ctedPrice <= 5 AND
dealmaster.stat us = 'offering';
END
Jun 27 '08 #2
CASE returns an expression, not logical condition. Here is how you can
change your query:

SELECT dealMasterId
FROM dealmaster AS D
INNER JOIN houses AS H
ON D.HouseId = H.HouseId
WHERE H.Location = @in_Location
AND H.HouseType = @in_HouseType
AND CASE WHEN @in_Size = 'within1000'
THEN CASE WHEN H.Size <= 1000 THEN 'T' END
WHEN @in_Size = 'more1000'
THEN CASE WHEN H.Size 1000 THEN 'T' END
ELSE CASE WHEN H.size 0 THEN 'T' END
END = 'T'
AND CASE WHEN @in_ExpectedPri ce = 'within5m'
THEN CASE WHEN H.expectedPrice <= 5 THEN 'T' END
WHEN @in_ExpectedPri ce = 'within10m'
THEN CASE WHEN H.expectedPrice <= 10 THEN 'T' END
WHEN @in_ExpectedPri ce = 'dontcare'
THEN CASE WHEN H.expectedPrice 0 THEN 'T' END
ELSE CASE WHEN H.expectedPrice 0 THEN 'T' END
END = 'T'
AND H.EntityStatus = 'A'
AND D.EntityStatus = 'A'
AND D.expectedPrice <= 5
AND D.status = 'offering';
HTH,

Plamen Ratchev
http://www.SQLStudio.com

Jun 27 '08 #3

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

Similar topics

19
8086
by: Robert Scheer | last post by:
Hi. In VBScript I can use a Select Case statement like that: Select Case X Case 1 to 10 'X is between 1 and 10 Case 11,14,16 'X is 11 or 14 or 16 End Select
2
3199
by: cs168 | last post by:
Hi I am new in ASP programming so I do use the very basic and simple way to do all my stuff. Now I do really got stuck at how can I loop thru the calculation for all my selection.. My full code is as below:- <% tBegin = request("beginww") tEnd = request("endww") tYear = request("wwyear") %> <body>
10
2162
by: MLH | last post by:
Suppose the following... Dim A as Date A=#7/24/2005# I wish to compare value of A against 2 other values: 1) 8/1/2005 2) 9/1/2005 Which is better and why... First:
7
9328
by: Lauren Quantrell | last post by:
Is there any speed/resource advantage/disadvantage in using Select Case x Case 1 Case 2 etc. many more cases... End Select VS.
3
3390
by: mark.irwin | last post by:
Hello all, Have an issue where a redirect pushes data to a page with a select case which then redirects to another page. Problem is the redirect isnt working in 1 case. Code below: strURL = "" if i = 1 then strURL = "redirect.aspx?page=APIQ&parcel=" & strParcel &
1
1509
by: tony | last post by:
Hello!! I have the method CollectData that I want to use Reflection on. I use foreach looping through a collection of DSRow. This DSRow does not have a property but I can add one if it would solve my problem. After each lap in the foreach loop I instansiate a Composition object. In the switch case part I have these statements case "EAF" : case "C_min" : case "C_aim" :
6
1930
by: graeme g | last post by:
hi how would i write the following in switch case statement: if (x < 40) y = 0; else if (x < 65) y = 1; else if (x < 80) y = 2;
1
21666
by: microsoft.public.dotnet.languages.vb | last post by:
Hi All, I wanted to know whether this is possible to use multiple variables to use in the select case statement such as follows: select case dWarrExpDateMonth, dRetailDateMonth case "01" : dWarrExpDateMonth="Jan" : dRetailDateMonth="Jan" case "02" : dWarrExpDateMonth="Feb" : dRetailDateMonth="Feb" End Select
22
3160
by: John | last post by:
Hi Folks, I'm experimenting a little with creating a custom CEdit control so that I can decide on what the user is allowed to type into the control. I started off only allowing floating point numbers then added support for putting in lat/lon coordinates. I tried this little piece of code inside the OnChar function but compiler complained about missing ';' after "case _T('W'):"
24
3110
by: clockworx05 | last post by:
Hey guys i have this program that i need to write for class. here are the instructions: Write a function called foo that asks the user for their age. Pass the age value to a function called showAge that uses a select case structure to print out the following: If the age is 0-12 print out "You are still a child. If the age is 13 - 19 print out "You are a teenager. If the age is 20 - 40 print "You are an adult" anything else print "You are...
0
8459
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8890
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8577
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8653
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7398
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6206
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5677
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4376
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2786
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

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.