tags:

views:

50

answers:

2

Hi, 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)
+1  A: 

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

Ivo
Basically there can only be one Prestige-Featured property so if the user has checked that checkbox in my edit ui then I want to set the column isprestigefeatured = 1 for that row where propertyid = propertyid and for all the other rows I want to set isprestigefeatured = 0
Andrew Welch
A: 
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)
Veer
Can you give me a full example based on my sql query above, as I've just stupidly managed to delete all of my test data using above example
Andrew Welch
check my update. Take a backup of your test data and run your procedure.
Veer
I don't want to update the values into all of the SET columns if isprestigefeatured = 0. If isprestigefeatured = 0 I just want to update "0" into the column of isprestigefeatured if the propertyid <> propertyid. How do I restrict the update to just that column.
Andrew Welch
check out this post: http://forums.asp.net/t/1546148.aspx
Andrew Welch
Is that ok?............
Veer
Hi 'Veer'. Thanks for the help. I'm still a bit confused to be honest. Have a read of the post I put on the asp.net forums... The link is there. I'm confused as to what they mean on there also! Going to look into how to 'wrap both update statements with a begin transaction and a commit statement'
Andrew Welch
I already have seen your post. I really don't understand where you've two update statement. Did you try my code? If it is not working can you elaborate your need?
Veer