更新存储过程中的表

时间:2016-03-16 05:32:42

标签: sql-server

我有一个包含参数的存储过程。如果我传递参数,则查询将按LocationID更新表。

存储过程如下:

CREATE proc [Location].[SPUpadtelocation_by_locationID]
    @Name       nvarchar(100),
    @Parentcode nvarchar(10),
    @CLientcode nvarchar(10),
    @Address    nvarchar(100),
    @city       nvarchar(20),
    @state      nvarchar(20),
    @Pin        int,
    @Clientid   int,
    @Status     bit,
    @Fax        int,
    @CreatedAt  date,
    @AccessAt   datetime,
    @Accessby   int,
    @LocationID int 
as
begin 
     if (select LocationID 
         from Location.tbl_Locationmaster 
         where Parentcode = @Parentcode 
           and Clientcode = @Clientcode  
           and Clientid = @Clientid) = @LocationID --check Location allready exists or not 
     begin 
         UPDATE [Location].[tbl_Locationmaster]
         SET [Name] = @Name,
             [Parentcode] = @Parentcode,
             [Clientcode] = @CLientcode,
             [Address] = @Address,
             [City] = @city,
             [State] = @state, 
             [Pin] = @Pin,
             [Clientid] = @Clientid, 
             [Status] = @Status,
             [Fax] = @Fax,
             [AccessAt] = getdate(),
             [Accessby] = @Accessby
         WHERE 
             LocationID = @LocationID
     end
end
GO

如果Name和{{1},我不想更新ParentCode表的ClientCode列,Location列和Parentcode列已经在同一Clientcode的表中。但是应该更新列的其余部分。

2 个答案:

答案 0 :(得分:0)

查看MSDN上的COALESCECASE语句。它们都提供了对特定列进行条件UPDATE的示例。

答案 1 :(得分:0)

如果每个客户有一个条目,则IF逻辑应如下所示

if (select LocationID from Location.tbl_Locationmaster where Parentcode=@Parentcode and Clientcode =@Clientcode  and Clientid=@Clientid) = @LocationID --check Location already exists or not 
begin 
    UPDATE [Location].[tbl_Locationmaster]
        SET
            [Name] = COALESCE([Name],@Name)
            ,[Parentcode] =COALESCE([Parentcode],@Parentcode)
            ,[Clientcode] = COALESCE([Clientcode],@CLientcode)
            ,[Address] = @Address
            ,[City] = @city
            ,[State] = @state
            ,[Pin] = @Pin
            ,[Clientid] = @Clientid
            ,[Status] = @Status
            ,[Fax] = @Fax
            ,[AccessAt] = getdate()
            ,[Accessby] = @Accessby
    WHERE LocationID = @LocationID
end

如果您有相同client ID的多个位置行,并且您不希望更新当前位置行,在这种情况下使用新数据,您的逻辑应该是

if (select LocationID from Location.tbl_Locationmaster where Parentcode=@Parentcode and Clientcode =@Clientcode  and Clientid=@Clientid) = @LocationID --check Location already exists or not 
begin 
    UPDATE L
        SET
            [Name] = CASE T.[Name] IS NOT NULL THEN L.[NAME] ELSE @Name END
            ,[Parentcode] =CASE T.[Parentcode] IS NOT NULL THEN L.[ParentCode] ELSE @Parentcode END
            ,[Clientcode] = CASE T.[Clientcode] IS NOT NULL THEN L.[Clientcode] ELDE @CLientcode END
            ,[Address] = @Address
            ,[City] = @city
            ,[State] = @state
            ,[Pin] = @Pin
            ,[Clientid] = @Clientid
            ,[Status] = @Status
            ,[Fax] = @Fax
            ,[AccessAt] = getdate()
            ,[Accessby] = @Accessby
    FROM [Location].[tbl_Locationmaster] L
    LEFT JOIN (SELECT Name, ParentCode,ClientCode FROM [Location].[tbl_Locationmaster] ) T
ON T.[Clientid] = @Clientid 
WHERE L.LocationID = @LocationID 
end