Northwind Database (Document! X Sample)
AdventureWorks Database / dbo Schema / dbo.ufnGetStock User Defined Function
In This Topic
    dbo.ufnGetStock User Defined Function
    In This Topic
    Description
    Scalar function returning the quantity of inventory in LocationID 6 (Miscellaneous Storage)for a specified ProductID.
    Properties
    Creation Date27/10/2017 14:33
    Encrypted
    Ansi Nulls
    Parameters
    ParameterDirectionDescriptionData TypeSize
    InInput parameter for the scalar function ufnGetStock. Enter a valid ProductID from the Production.ProductInventory table.Integer4
    Return Value Integer4
    Objects that dbo.ufnGetStock depends on
     Database ObjectObject TypeDescriptionDep Level
    dbo.Flag datatypedbo.FlagUser Defined Data Type 4
    Production.Location tableProduction.LocationTableProduct inventory and manufacturing locations.1
    dbo.Name datatypedbo.NameUser Defined Data Type 2
    Production.Product tableProduction.ProductTableProducts sold or used in the manfacturing of sold products.1
    Production.ProductCategory tableProduction.ProductCategoryTableHigh-level product categorization.3
    Production.ProductInventory tableProduction.ProductInventoryTableProduct inventory information.2
    Production.ProductModel tableProduction.ProductModelTableProduct model classification.2
    Production.ProductSubcategory tableProduction.ProductSubcategoryTableProduct subcategories. See ProductCategory table.2
    Production.UnitMeasure tableProduction.UnitMeasureTableUnit of measure lookup table.2
    Procedure Source Code
    CREATE FUNCTION [dbo].[ufnGetStock](@ProductID [int])
    RETURNS [int] 
    AS 
    
    -- Returns the stock level for the product. This function is used internally only
    BEGIN
        DECLARE @ret int;
        
        SELECT @ret = SUM(p.[Quantity]) 
        FROM [Production].[ProductInventory] p 
        WHERE p.[ProductID] = @ProductID 
            AND p.[LocationID] = '6'; -- Only look at inventory in the misc storage
        
        IF (@ret IS NULL) 
            SET @ret = 0
        
        RETURN @ret
    END;
    
    See Also