2010-04-12 69 views
1

我有一個查詢下面。我想添加一個條件,如果參數@IsPrestigeFeatured = 0然後更新IsprestigeFeatured列中的所有其他行<>PropertyId爲0,並且如果@IsPrestigeFeatured = 1然後IsprestigeFeatured = 1 where propertyId = PropertyId更新所有其他記錄

我查看了case語句/ if語句,但我似乎無法獲得正確的語法。 歡呼

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) 
+0

什麼數據庫/語言? MS SQL,mySQL,Oracle? – Ivo 2010-04-12 08:52:57

+0

我正在使用MSSQL – weaveoftheride 2010-04-12 08:59:24

回答

0
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) 
+0

你能給我一個完整的例子基於我上面的SQL查詢,因爲我只是愚蠢地設法刪除我所有的測試數據使用上面的例子 – weaveoftheride 2010-04-12 11:40:05

+0

檢查我的更新。備份您的測試數據並運行您的程序。 – Amsakanna 2010-04-12 11:51:33

+0

如果isprestigefeatured = 0,我不想將值更新到所有SET列中。如果isprestigefeatured = 0,我只是想將「0」更新到isprestigefeatured列(如果propertyid <> propertyid)。如何限制更新到該列。 – weaveoftheride 2010-04-12 11:55:08