本文介紹了在 SQL 中調用動態函數名的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
是否可以在 SQL 中調用具有動態名稱的函數?
Is it possible to call a function with a dynamic name in SQL?
例如:
SELECT functionid, (SELECT results FROM dbo.Function_*functionid*) AS results
FROM List_of_Functions
這將為表 List_of_Functions 中的每一行調用不同的函數.
This would call a different function for every row in the table List_of_Functions.
還是我的想法都錯了?
推薦答案
您將需要構建(輸入或基于您的表動態構建)SQL 語句,如:
You will need to build (either type it in, or build it dynamically based on your table) a SQL statement like:
SELECT
functionid
,CASE functionid
WHEN 1 THEN dbo.Function_1()
WHEN 2 THEN dbo.Function_2()
WHEN 3 THEN dbo.Function_3()
END AS results
FROM List_of_Functions
與其構建所有這些函數,不如構建一個函數,然后傳入一個函數可以用來區分處理的值,這不是更好嗎?喜歡:
Instead of building all those functions, wouldn't it be better to build one function, and pass in a value that the function can use to differentiate the processing? like:
SELECT
functionid
,dbo.Function(functionid) AS results
FROM List_of_Functions_Parameters
這篇關于在 SQL 中調用動態函數名的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!