zoukankan      html  css  js  c++  java
  • sqlserver~创建函数

    1.创建标量函数

    即返回一个单个值的函数

    定义如下

    IF OBJECT_ID (N'dbo.ufnGetInventoryStock', N'FN') IS NOT NULL  
        DROP FUNCTION ufnGetInventoryStock;  
    GO  
    CREATE FUNCTION dbo.ufnGetInventoryStock(@ProductID int)  
    RETURNS int   
    AS   
    -- Returns the stock level for the product.  
    BEGIN  
        DECLARE @ret int;  
        SELECT @ret = SUM(p.Quantity)   
        FROM Production.ProductInventory p   
        WHERE p.ProductID = @ProductID   
            AND p.LocationID = '6';  
         IF (@ret IS NULL)   
            SET @ret = 0;  
        RETURN @ret;  
    END;

    使用示例

    SELECT ProductModelID, Name, dbo.ufnGetInventoryStock(ProductID)AS CurrentSupply  
    FROM Production.Product  
    WHERE ProductModelID BETWEEN 75 and 80;

    2. 创建表值函数

    即返回的是一个列表集合

    定义如下

    IF OBJECT_ID (N'Sales.ufn_SalesByStore', N'IF') IS NOT NULL  
        DROP FUNCTION Sales.ufn_SalesByStore;  
    GO  
    CREATE FUNCTION Sales.ufn_SalesByStore (@storeid int)  
    RETURNS TABLE  
    AS  
    RETURN   
    (  
        SELECT P.ProductID, P.Name, SUM(SD.LineTotal) AS 'Total'  
        FROM Production.Product AS P   
        JOIN Sales.SalesOrderDetail AS SD ON SD.ProductID = P.ProductID  
        JOIN Sales.SalesOrderHeader AS SH ON SH.SalesOrderID = SD.SalesOrderID  
        JOIN Sales.Customer AS C ON SH.CustomerID = C.CustomerID  
        WHERE C.StoreID = @storeid  
        GROUP BY P.ProductID, P.Name  
    );

    使用示例

    SELECT * FROM Sales.ufn_SalesByStore (602);

    表值函数示例2:

    定义如下

    IF OBJECT_ID (N'dbo.ufn_FindReports', N'TF') IS NOT NULL  
        DROP FUNCTION dbo.ufn_FindReports;  
    GO  
    CREATE FUNCTION dbo.ufn_FindReports (@InEmpID INTEGER)  
    RETURNS @retFindReports TABLE   
    (  
        EmployeeID int primary key NOT NULL,  
        FirstName nvarchar(255) NOT NULL,  
        LastName nvarchar(255) NOT NULL,  
        JobTitle nvarchar(50) NOT NULL,  
        RecursionLevel int NOT NULL  
    )  
    --Returns a result set that lists all the employees who report to the   
    --specific employee directly or indirectly.*/  
    AS  
    BEGIN  
    WITH EMP_cte(EmployeeID, OrganizationNode, FirstName, LastName, JobTitle, RecursionLevel) -- CTE name and columns  
        AS (  
            SELECT e.BusinessEntityID, e.OrganizationNode, p.FirstName, p.LastName, e.JobTitle, 0 -- Get the initial list of Employees for Manager n  
            FROM HumanResources.Employee e   
    INNER JOIN Person.Person p   
    ON p.BusinessEntityID = e.BusinessEntityID  
            WHERE e.BusinessEntityID = @InEmpID  
            UNION ALL  
            SELECT e.BusinessEntityID, e.OrganizationNode, p.FirstName, p.LastName, e.JobTitle, RecursionLevel + 1 -- Join recursive member to anchor  
            FROM HumanResources.Employee e   
                INNER JOIN EMP_cte  
                ON e.OrganizationNode.GetAncestor(1) = EMP_cte.OrganizationNode  
    INNER JOIN Person.Person p   
    ON p.BusinessEntityID = e.BusinessEntityID  
            )  
    -- copy the required columns to the result of the function   
       INSERT @retFindReports  
       SELECT EmployeeID, FirstName, LastName, JobTitle, RecursionLevel  
       FROM EMP_cte   
       RETURN  
    END;  
    GO

    使用示例

    SELECT EmployeeID, FirstName, LastName, JobTitle, RecursionLevel  
    FROM dbo.ufn_FindReports(1);
  • 相关阅读:
    12345679*81=?
    怪异,漂亮的几个数学恒等式(转)
    道路着色问题
    一组数学算式的欣赏(转)
    数学中奇妙的“金蝉脱壳”(转)
    数学中的分分合合(转)
    四方定理和卡布列克常数(转)
    简单的题目 有趣的现象
    Android学习笔记 第三节 基本控件学习
    Android学习笔记 第二节 HelloWorld程序
  • 原文地址:https://www.cnblogs.com/Vincent-yuan/p/12963086.html
Copyright © 2011-2022 走看看