case-when error
am 15.04.2008 11:27:31 von majorone
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
Re: case-when error
am 15.04.2008 15:48:20 von Tom van Stiphout
On Tue, 15 Apr 2008 02:27:31 -0700 (PDT), philip
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
Re: case-when error
am 15.04.2008 15:50:54 von Plamen Ratchev
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