篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql MS SQL表值函数相关的知识,希望对你有一定的参考价值。
CREATE FUNCTION dbo.GetProductsByCategory (@CategoryID INT)
RETURNS TABLE
AS
RETURN
(SELECT P.ProductID,
P.Name AS ProductName,
P.ProductNumber,
P.ListPrice,
PC.Name AS Category,
PSC.Name AS SubCategory
FROM Production.Product P
INNER JOIN Production.ProductSubcategory PSC
ON P.ProductSubcategoryID = PSC.ProductSubcategoryID
INNER JOIN Production.ProductCategory PC
ON PSC.ProductCategoryID = PC.ProductCategoryID
WHERE P.ProductSubcategoryID IS NOT NULL
AND PC.ProductCategoryID = @CategoryID)
-- EXECUTION
SELECT ProductName, ProductNumber, SubCategory
FROM dbo.GetProductsByCategory(1);