代码之家  ›  专栏  ›  技术社区  ›  goric

从.NET调用表值SQL函数

  •  28
  • goric  · 技术社区  · 16 年前

    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;
    

    我还知道表值函数可以以类似的方式调用,例如:

    String query = "select * from testFunction(param1,...)"; //testFunction is table-valued
    SqlCommand cmd = new SqlCommand(query, sqlConn);
    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
    adapter.Fill(tbl);
    

    我的问题是,表值函数能否像标量值函数一样作为存储过程调用?(例如,使用正在调用的表值函数复制我的第一个代码段,并通过ReturnValue参数获取返回的表)。

    1 回复  |  直到 11 年前
        1
  •  19
  •   athspk RNJ    12 年前

    否,因为您需要选择它们。但是,您可以创建一个存储的过程包装器,这可能会破坏拥有表函数的意义。