Northwind Database (Document! X Sample)
dbo.ufnGetStock User Defined Function
AdventureWorks Database > dbo Schema : dbo.ufnGetStock User Defined Function
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 3
Production.Location tableProduction.LocationTableProduct inventory and manufacturing locations.2
dbo.Name datatypedbo.NameUser Defined Data Type 3
Production.Product tableProduction.ProductTableProducts sold or used in the manfacturing of sold products.2
Production.ProductCategory tableProduction.ProductCategoryTableHigh-level product categorization.4
Production.ProductInventory tableProduction.ProductInventoryTableProduct inventory information.1
Production.ProductModel tableProduction.ProductModelTableProduct model classification.3
Production.ProductSubcategory tableProduction.ProductSubcategoryTableProduct subcategories. See ProductCategory table.3
Production.UnitMeasure tableProduction.UnitMeasureTableUnit of measure lookup table.3
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

Related Objects

dbo Schema
AdventureWorks Database