ID付きのcsv文字列である入力に基づいて、cteで再帰的な選択を実行するSqlServer関数があります。
残念ながら、関数内で "option(maxrecursion 0)" を使用することはできません。関数の実行時に使用する必要があります。問題は、EntityFramework の EntitySql でこのオプションを使用する方法が見つからないことです。
私の関数が呼ばれていることを考慮してMyRecursiveFunction
、ここにいくつかのコードスニペットがあります:
public virtual IQueryable<MyFunctionReturnType> ExecuteMyFunction(IObjectContextAdapter objContextAdapter, string csvIds)
{
var idsParam = new ObjectParameter("idsParam", csvIds);
// This is the original one, that works, but has no "option(maxrecursion 0)"
return objContextAdapter.CreateQuery<MyFunctionReturnType>("[MyRecursiveFunction](@idsParam)", idsParam);
// gives me an error of incorrect syntax near "option"
return objContextAdapter.CreateQuery<MyFunctionReturnType>("select VALUE tblAlias from [MyRecursiveFunction](@idsParam) as tblAlias OPTION(MAXRECURSION 0)", idsParam);
// Also gives me syntax error:
return objContextAdapter.CreateQuery<MyFunctionReturnType>("MyRecursiveFunction(@idsParam) option(maxrecursion 0)", idsParam);
}
option(maxrecursion 0)
entitySqlでの使用方法を知っている人はいますか?
「ExecuteStoreQuery」を使用して必要な SQL クエリを実行できることはわかっていますが、この「ExecuteMyFunction」の戻り値は実体化の前に別の IQueryable と結合されるため、IQueryable が必要です。
時間を節約し、 ... とExecuteStoreQuery
一緒に呼び出すことを提案しないでください。AsQueryable
ページングの結果を 10 個だけ具体化するので、結果セット全体を具体化したくありません。
これが私の TVF の表現です。
-- Consider that I have the given table for executing this function.
-- This table has a foreign key to itself, as the data represents a tree, like an organization chart
CREATE TABLE MyTable
(
Id INT,
ParentId INT, -- FK to 'MyTable'.'Id',
Name VARCHAR(400)
)
-- Here is my function definition:
CREATE FUNCTION MyRecursiveFunction (@idsParam VARCHAR(MAX))
RETURNS TABLE
AS
RETURN
(
-- create a cte for recursively getting the data
with myCte (id, parentId) as
(
SELECT tbl.Id, tbl.ParentId FROM MyTable AS tbl
-- This function just transform the varchar into a table of "Value"
INNER JOIN [dbo].[SplitTextIntoTableOfInt](@idsParam, ',') AS ids ON a.ParentId = ids.Value
UNION ALL
SELECT a.Id, a.ParentId FROM myCte AS parent
INNER JOIN MyTable tbl ON tbl.ParentId = parent.Id
)
SELECT * FROM myCte -- I can't use 'option(maxrecursion 0)' in here
)