sql中如何写函数?

sql中如何写函数


手掌心
浏览 918回答 2
2回答

烙印99

/*获取两个数值的大小0:小于;1:大于;2:等于*/CREATE function GetMax(@qty1 varchar(50),@qty2 varchar(50))returns intbegindeclare @Num intif (convert(decimal(18,5),@qty1)-convert(decimal(18,5),@qty2) >0)beginset @Num=1endelse if (convert(decimal(18,5),@qty1)-convert(decimal(18,5),@qty2) <0)beginset @Num=0endelsebeginset @Num=2endreturn @Num

米脂

打开SQL,然后按F1,然后搜索CREATE FUNCTION,说明很详细,自己看吧。CREATE FUNCTION Sales.fn_SalesByStore (@storeid int)RETURNS TABLEASRETURN(SELECT P.ProductID, P.Name, SUM(SD.LineTotal) AS 'YTD Total'FROM Production.Product AS PJOIN Sales.SalesOrderDetail AS SD ON SD.ProductID = P.ProductIDJOIN Sales.SalesOrderHeader AS SH ON SH.SalesOrderID = SD.SalesOrderIDWHERE SH.CustomerID = @storeidGROUP BY P.ProductID, P.Name);GO
打开App,查看更多内容
随时随地看视频慕课网APP