473,386 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 spDealMasterSearch, 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_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[spDealMasterSearch]

@in_Location varchar(50),
@in_HouseType varchar(50),
@in_Size varchar(50),
@in_ExpectedPrice varchar(50)

AS
BEGIN

SELECT dealMasterId From
dealmaster INNER JOIN houses
ON dealmaster.HouseId = houses.HouseId
WHERE
houses.Location = @in_Location AND
houses.HouseType = @in_HouseType AND
houses.Size

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

houses.expectedPrice
CASE
WHEN @in_ExpectedPrice = 'within5m' THEN <= 5
WHEN @in_ExpectedPrice = 'within10m' THEN <=10
WHEN @in_ExpectedPrice = 'dontcare' THEN 0
ELSE 0
END AND

houses.EntityStatus = 'A' AND
dealmaster.EntityStatus = 'A' AND
dealmaster.expectedPrice <= 5 AND
dealmaster.status = 'offering';
END
Jun 27 '08 #1
2 1686
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 spDealMasterSearch, 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_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[spDealMasterSearch]

@in_Location varchar(50),
@in_HouseType varchar(50),
@in_Size varchar(50),
@in_ExpectedPrice varchar(50)

AS
BEGIN

SELECT dealMasterId From
dealmaster INNER JOIN houses
ON dealmaster.HouseId = houses.HouseId
WHERE
houses.Location = @in_Location AND
houses.HouseType = @in_HouseType AND
houses.Size

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

houses.expectedPrice
CASE
WHEN @in_ExpectedPrice = 'within5m' THEN <= 5
WHEN @in_ExpectedPrice = 'within10m' THEN <=10
WHEN @in_ExpectedPrice = 'dontcare' THEN 0
ELSE 0
END AND

houses.EntityStatus = 'A' AND
dealmaster.EntityStatus = 'A' AND
dealmaster.expectedPrice <= 5 AND
dealmaster.status = '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_ExpectedPrice = 'within5m'
THEN CASE WHEN H.expectedPrice <= 5 THEN 'T' END
WHEN @in_ExpectedPrice = 'within10m'
THEN CASE WHEN H.expectedPrice <= 10 THEN 'T' END
WHEN @in_ExpectedPrice = '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
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
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...
10
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
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
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 =...
1
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...
6
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
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...
22
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...
24
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...
0
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...
0
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...
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
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,...
0
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...

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.