updating all other records
I've got a query below. I want to add a condition that if the parameter of @IsPrestigeFeatured = 0
then update all of the other rows <> PropertyId
in the IsprestigeFeatured column to 0 and if @IsPrestigeFeatured = 1
then IsprestigeFeatured = 1 where propertyId = PropertyId
.
I'开发者_运维问答ve looked into case statements / if statements but I can't seem to get the syntax right. cheers
ALTER PROCEDURE dbo.Update_Property
@propertyId int,
@propertyTypeId int,
@Name ntext,
@Price int,
@DescriptionResultsExcerpt text,
@Description ntext,
@Characteristics ntext,
@IsRenovation int,
@IsCharacter int,
@IsPrestige int,
@IsHomepageFeatured int,
@IsPrestigeFeatured int,
@CityId int,
@DepartmentId int,
@CommuneId int
As
UPDATE Property
SET Name = @Name, PropertyTypeID = @propertyTypeId, Price = @Price,
DescriptionResultsExcerpt = @DescriptionResultsExcerpt,
Description = @Description, Characteristics = @Characteristics,
IsRenovation = @IsRenovation, IsCharacter = @IsCharacter,
IsPrestige = @IsPrestige, IsHomepageFeatured = @IsHomepageFeatured,
IsPrestigeFeatured = @IsPrestigeFeatured, CityId = @CityId,
DepartmentId = @DepartmentId, CommuneId = @CommuneId
FROM Property
WHERE (PropertyId = @PropertyId)
If you use TSQL, check out http://www.databasejournal.com/features/mssql/article.php/3087431/T-SQL-Programming-Part-1---Defining-Variables-and-IFELSE-logic.htm
ALTER PROCEDURE dbo.Update_Property
@propertyId int, @propertyTypeId int, @Name ntext, @Price int,
@DescriptionResultsExcerpt text, @Description ntext,
@Characteristics ntext, @IsRenovation int, @IsCharacter int,
@IsPrestige int, @IsHomepageFeatured int,
@IsPrestigeFeatured int, @CityId int,
@DepartmentId int, @CommuneId int
As
UPDATE
Property
SET IsPrestigeFeatured = @IsPrestigeFeatured
WHERE
(@IsPrestigeFeatured = 0 AND PropertyId <> @PropertyId) OR
(@IsPrestigeFeatured = 1 AND PropertyId = @PropertyId)
精彩评论