篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql MS SQL多语句表值函数相关的知识,希望对你有一定的参考价值。
CREATE FUNCTION dbo.GetAdventureBikes ()
RETURNS @Product TABLE (
ProductID INT,
ProductName NVARCHAR(50),
ProductNumber NVARCHAR(25),
SubCategory NVARCHAR(50))
AS
BEGIN
INSERT @Product
(ProductID,
ProductName,
ProductNumber,
SubCategory)
SELECT P.ProductID,
P.Name AS ProductName,
P.ProductNumber,
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 = 1;
INSERT @Product
(ProductID,
ProductName,
ProductNumber,
SubCategory)
SELECT P.ProductID,
P.Name AS ProductName,
P.ProductNumber,
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 = 3;
RETURN
END
-- EXECUTION
SELECT ProductName,
ProductNumber,
SubCategory
FROM dbo.GetAdventureBikes();