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]
[at] in_Location varchar(50),
[at] in_HouseType varchar(50),
[at] in_Size varchar(50),
[at] in_ExpectedPrice varchar(50)
AS
BEGIN
SELECT dealMasterId From
dealmaster INNER JOIN houses
ON dealmaster.HouseId = houses.HouseId
WHERE
houses.Location = [at] in_Location AND
houses.HouseType = [at] in_HouseType AND
houses.Size
CASE
WHEN [at] in_Size = 'within1000' THEN <= 1000
WHEN [at] in_Size = 'more1000' THEN > 1000
ELSE > 0
END AND
houses.expectedPrice
CASE
WHEN [at] in_ExpectedPrice = 'within5m' THEN <= 5
WHEN [at] in_ExpectedPrice = 'within10m' THEN <=10
WHEN [at] 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
Re: case-when error
On Tue, 15 Apr 2008 02:27:31 -0700 (PDT), philip <majorone [at] 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]
>
> [at] in_Location varchar(50),
> [at] in_HouseType varchar(50),
> [at] in_Size varchar(50),
> [at] in_ExpectedPrice varchar(50)
>
>AS
>BEGIN
>
> SELECT dealMasterId From
> dealmaster INNER JOIN houses
> ON dealmaster.HouseId = houses.HouseId
> WHERE
> houses.Location = [at] in_Location AND
> houses.HouseType = [at] in_HouseType AND
> houses.Size
>
> CASE
> WHEN [at] in_Size = 'within1000' THEN <= 1000
> WHEN [at] in_Size = 'more1000' THEN > 1000
> ELSE > 0
> END AND
>
> houses.expectedPrice
> CASE
> WHEN [at] in_ExpectedPrice = 'within5m' THEN <= 5
> WHEN [at] in_ExpectedPrice = 'within10m' THEN <=10
> WHEN [at] 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
Re: case-when error
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 = [at] in_Location
AND H.HouseType = [at] in_HouseType
AND CASE WHEN [at] in_Size = 'within1000'
THEN CASE WHEN H.Size <= 1000 THEN 'T' END
WHEN [at] 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 [at] in_ExpectedPrice = 'within5m'
THEN CASE WHEN H.expectedPrice <= 5 THEN 'T' END
WHEN [at] in_ExpectedPrice = 'within10m'
THEN CASE WHEN H.expectedPrice <= 10 THEN 'T' END
WHEN [at] 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