DBA不喜歡我正在使用case語句和子查詢。我可以採取另一種方法來提高性能嗎?此更新語句是存儲過程的一部分。改進此SQL查詢以獲得更好的性能
UPDATE dbo.CMN_PersonsFerpa
SET
IsWorkEmailFerpa = (CASE WHEN (SELECT EmailType FROM dbo.CMN_PersonsEmailLinks WHERE CMN_PersonsEmailLinksID = @CMN_PersonsEmailLinksID) = 'Work' THEN @IsFERPA ELSE IsWorkEmailFerpa END,
IsPersonalEmailFerpa = (CASE WHEN (SELECT EmailType FROM dbo.CMN_PersonsEmailLinks WHERE CMN_PersonsEmailLinksID = @CMN_PersonsEmailLinksID) = 'Personal' THEN @IsFERPA ELSE IsPersonalEmailFerpa END,
IsParentEmailFerpa = (CASE WHEN (SELECT EmailType FROM dbo.CMN_PersonsEmailLinks WHERE CMN_PersonsEmailLinksID = @CMN_PersonsEmailLinksID) = 'Parent' THEN @IsFERPA ELSE IsParentEmailFerpa END,
IsTempEmailFerpa = (CASE WHEN (SELECT EmailType FROM dbo.CMN_PersonsEmailLinks WHERE CMN_PersonsEmailLinksID = @CMN_PersonsEmailLinksID) = 'Temporary' THEN @IsFERPA ELSE IsTempEmailFerpa END,
IsFAFSAEmailFerpa = (CASE WHEN (SELECT EmailType FROM dbo.CMN_PersonsEmailLinks WHERE CMN_PersonsEmailLinksID = @CMN_PersonsEmailLinksID) = 'FAFSA' THEN @IsFERPA ELSE IsFAFSAEmailFerpa END,
IsCSSProfEmailFerpa = (CASE WHEN (SELECT EmailType FROM dbo.CMN_PersonsEmailLinks WHERE CMN_PersonsEmailLinksID = @CMN_PersonsEmailLinksID) = 'CSS Profile' THEN @IsFERPA ELSE IsCSSProfEmailFerpa END,
IsCommenceEmailFerpa = (CASE WHEN (SELECT EmailType FROM dbo.CMN_PersonsEmailLinks WHERE CMN_PersonsEmailLinksID = @CMN_PersonsEmailLinksID) = 'Commencement' THEN @IsFERPA ELSE IsCommenceEmailFerpa END,
IsAcctHoldEmailFerpa = (CASE WHEN (SELECT EmailType FROM dbo.CMN_PersonsEmailLinks WHERE CMN_PersonsEmailLinksID = @CMN_PersonsEmailLinksID) = 'Account Holder' THEN @IsFERPA ELSE IsAcctHoldEmailFerpa END
Where CMN_PersonsFerpa.cmn_personsID = (select cmn_personsID from CMN_PersonsEmailLinks WHERE CMN_PersonsEmailLinksID = @CMN_PersonsEmailLinksID)
在這裏有幾個建議是我有。這可以接受嗎?還是有更好的辦法?:
SELECT @DBType = EmailType, @cmn_personsID = CMN_PersonsID FROM dbo.CMN_PersonsEmailLinks WHERE CMN_PersonsEmailLinksID = @CMN_PersonsEmailLinksID
UPDATE dbo.CMN_PersonsFerpa
SET
IsWorkEmailFerpa = CASE WHEN @DBType = 'Work' THEN @IsFERPA END,
IsPersonalEmailFerpa = CASE WHEN @DBType = 'Personal' THEN @IsFERPA END,
IsParentEmailFerpa = CASE WHEN @DBType = 'Parent' THEN @IsFERPA END,
IsTempEmailFerpa = CASE WHEN @DBType = 'Temporary' THEN @IsFERPA END,
IsFAFSAEmailFerpa = CASE WHEN @DBType = 'FAFSA' THEN @IsFERPA END,
IsCSSProfEmailFerpa = CASE WHEN @DBType = 'CSS Profile' THEN @IsFERPA END,
IsCommenceEmailFerpa = CASE WHEN @DBType = 'Commencement' THEN @IsFERPA END,
IsAcctHoldEmailFerpa = CASE WHEN @DBType = 'Account Holder' THEN @IsFERPA END,
LastChangeBy = @UserGUID,
LastChangeDateTime = GETDATE()
Where CMN_PersonsFerpa.cmn_personsID = @CMN_PersonsID
@CMN_PersonsEmailLinksID從哪裏來?它是否從CMN_PersonsFerpa中拉出來? – Malk 2014-09-24 21:00:38
@CMN_PersonsEmailLinksID來自前端而不是任何表。 – NonProgrammer 2014-09-24 21:04:58