Aracılığıyla paylaş


Nasıl yapılır: Tablo Değerli Kullanıcı Tanımlı İşlevler Kullanma

Tablo değerli bir işlev tek bir satır kümesi döndürür (birden çok sonuç şekli döndürebilen saklı yordamlardan farklı olarak). Tablo değerli bir işlevin dönüş türü olduğundan Table, SQL'de herhangi bir yerde tablo değerli bir işlev kullanabilirsiniz. Bu işlevi tablo kullanabilirsiniz. Tablo değerli işlevi bir tablo gibi de işleyebilirsiniz.

Örnek 1

Aşağıdaki SQL işlevi açıkça bir TABLEdöndürdüğünü belirtir. Bu nedenle, döndürülen satır kümesi yapısı örtük olarak tanımlanır.

CREATE FUNCTION ProductsCostingMoreThan(@cost money)  
RETURNS TABLE  
AS  
RETURN  
    SELECT ProductID, UnitPrice  
    FROM Products  
    WHERE UnitPrice > @cost  

LINQ to SQL işlevi aşağıdaki gibi eşler:

[Function(Name="dbo.ProductsCostingMoreThan", IsComposable=true)]
public IQueryable<ProductsCostingMoreThanResult> ProductsCostingMoreThan([Parameter(DbType="Money")] System.Nullable<decimal> cost)
{
    return this.CreateMethodCallQuery<ProductsCostingMoreThanResult>(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), cost);
}
   <FunctionAttribute(Name:="dbo.ProductsCostingMoreThan", IsComposable:=True)> _
Public Function ProductsCostingMoreThan(<Parameter(DbType:="Money")> ByVal cost As System.Nullable(Of Decimal)) As IQueryable(Of ProductsCostingMoreThanResult)
       Return Me.CreateMethodCallQuery(Of ProductsCostingMoreThanResult)(Me, CType(MethodInfo.GetCurrentMethod, MethodInfo), cost)
   End Function

Örnek 2

Aşağıdaki SQL kodu, işlevin döndürdüğü tabloya katılabileceğinizi ve başka herhangi bir tabloda olduğu gibi davranabileceğinizi gösterir:

SELECT p2.ProductName, p1.UnitPrice  
FROM dbo.ProductsCostingMoreThan(80.50)  
AS p1 INNER JOIN Products AS p2 ON p1.ProductID = p2.ProductID  

LINQ to SQL'de sorgu aşağıdaki gibi işlenir:

        var q =
from p in db.ProductsCostingMoreThan(80.50m)
join s in db.Products on p.ProductID equals s.ProductID
select new { p.ProductID, s.UnitPrice };
    Dim q = _
From p In db.ProductsCostingMoreThan(80.5), p1 In db.Products _
Where p.ProductID = p1.ProductID _
Select p.ProductID, p1.UnitPrice

Ayrıca bkz.