C# 从 .NET 调用表值 SQL 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calling Table-Valued SQL Functions From .NET
提问by goric
Scalar-valued functions can be called from .NET as follows:
可以从 .NET 调用标量值函数,如下所示:
SqlCommand cmd = new SqlCommand("testFunction", sqlConn); //testFunction is scalar
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("retVal", SqlDbType.Int);
cmd.Parameters["retVal"].Direction = ParameterDirection.ReturnValue;
cmd.ExecuteScalar();
int aFunctionResult = (int)cmd.Parameters["retVal"].Value;
I also know that table-valued functions can be called in a similar fashion, for example:
我也知道可以以类似的方式调用表值函数,例如:
String query = "select * from testFunction(param1,...)"; //testFunction is table-valued
SqlCommand cmd = new SqlCommand(query, sqlConn);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(tbl);
My question is, can table-valued functions be called as stored procedures, like scalar-valued functions can? (e.g., replicate my first code snippet with a table-valued function being called and getting the returned table through a ReturnValue parameter).
我的问题是,表值函数可以像标量值函数一样被称为存储过程吗?(例如,使用被调用的表值函数并通过 ReturnValue 参数获取返回的表来复制我的第一个代码片段)。
采纳答案by Nick Berardi
No because you need to select them. However you can create a stored proc wrapper, which may defeat the point of having a table function.
不,因为您需要选择它们。但是,您可以创建一个存储的 proc 包装器,这可能会破坏具有表函数的意义。