簡體   English   中英

如何避免存儲過程的參數過多?

[英]How to avoid long list of arguments to a stored procedure?

我有一個存儲過程來更新兩個表中的某些值。 但是參數列表或要更新的值集已增加到10個參數,並且將來可能會更多。 如何處理?

CREATE DEFINER=`root`@`localhost` PROCEDURE `update_base_plan`(userId int,newPlanId int,nextPlanId int,maxCreditPulseAllocated int)
begin
        if userId is not null and newPlanId is not null and nextPlanId is not null 
                                               and maxCreditPulseAllocated is not null
              then
                update planallocation as pa
                left join subscriptioninfo as si 
                on pa.SubscriptionId = si.SubscriptionId
                left join plans as pl
                on pa.CurrentPlanId = pl.PlanId
                set pa.CurrentPlanId = newPlanId, pa.NextPlanId = nextPlanId,
                pa.MaxCreditPulseAllocated = maxCreditPulseAllocated
                where pl.Plan_Type = 'base' and 
                si.UserId = userId;
        end if;
    end$$

DELIMITER ;

從技術上講,當(存儲的)過程需要n個參數時,通常不會提供n個參數。

但是,在某些編程語言中,不是一次提供所有這些參數,而是提供了一個數組/字典/對象,變成了“一個參數”。 我不確定,如果這在mysql中可行,但是您可以使用json作為輸入並使mysql解壓縮它(或者您可以調用它)。 例如,請參閱http://dev.mysql.com/doc/refman/5.7/en/json-search-functions.html (及其兄弟頁面)。

根據數據類型,您可以進行類似於csv或換行符的其他編碼。 但是,如果位置溝通不夠充分,我建議不要使用位置爭論。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM