2015-05-19 74 views
1

我有一個包含自定義函數(函數名稱和參數列表)的表格,並將其用於計算產品成本的百分比。我需要做的就是SQL(SQL Server 2008)。基於自定義公式計算,存儲在db表中

這是爲了存儲自定義的表函數

CREATE TABLE #tCustomFormula (customformula_id int, customfunction varchar(50), customparameters varchar(100)) 

INSERT INTO #tCustomFormula(customformula_id, customfunction, customparameters) 
SELECT 1, 'xpCalcPercentage', 'customer_id,product_id,costperiod,productdate' 
-- xpCalcPercentage is custom formula to calculate cost of a product 
DROP TABLE #tCustomFormula 

計算是在存儲過程中,其中該查詢表與供給customformula_id和參數來存儲的函數列表來完成。

例如:

exec spExecCustomFormula 
    1, -- custom formula id from the table above 
    '1,22,888,''2015-01-01''' -- parameters 

OR,以匹配的參數列表在表

exec spExecCustomFormula 
    1, -- custom formula id from the table above 
    'customer_id=1,product_id=22,costperiod=888,productdate=''2015-01-01''' -- parameters 

什麼是定義該存儲過程的最佳方式?基本上,我需要將存儲過程的參數列表與來自表#tCustomFormula的customparameters字段進行匹配,構造executeSQL字符串並使用動態SQL執行它。

任何幫助非常感謝。

感謝, 吉納

====================================== ===================================

回答

2

兩者都不是特別好,因爲你需要解析一個字符串轉換爲一組值,這是SQL不太擅長的。

您似乎在詢問您嘗試的解決方案,而不是問題本身,即the xy problem。你可以添加一些關於更廣泛的圖片的信息,你爲什麼試圖在第一時間做到這一點?

相關問題