我有一个包含参数的存储过程。如果我传递参数,则查询将按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
的表中。但是应该更新列的其余部分。
答案 0 :(得分:0)
答案 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