例子,sql函数返回标量值。
sql语句,如下:
--创建数据表
6> CREATE TABLE Orders (
7> OrderID int NOT NULL ,
8> CustomerID nchar (5) NULL ,
9> EmployeeID int NULL ,
10> OrderDate datetime NULL ,
11> requiredDate datetime NULL ,
12> ShippedDate datetime NULL ,
13> ShipVia int NULL ,
14> Freight money NULL DEFAULT (0),
15> ShipName nvarchar (40) NULL ,
16> ShipAddress nvarchar (60) NULL ,
17> ShipCity nvarchar (15) NULL ,
18> ShipRegion nvarchar (15) NULL ,
19> ShipPostalCode nvarchar (10) NULL ,
20> ShipCountry nvarchar (15) NULL)
21> GO
1>
2> CREATE FUNCTION DayOnly(@Date datetime)
3> RETURNS varchar(12)
4> AS
5> BEGIN
6> RETURN CONVERT(varchar(12), @Date, 101)
7> END
8> GO
1> SELECT *
2> FROM Orders
3> WHERE dbo.DayOnly(OrderDate) = dbo.DayOnly(GETDATE())
4> GO
OrderID CustomerID EmployeeID OrderDate RequiredDate ShippedDate ShipVia Freight ShipName ShipAddress
ShipCity ShipRegion ShipPostalCode ShipCountry
----------- ---------- ----------- ----------------------- ----------------------- ----------------------- ----------- --------------------- ---------------------------------------- ------------------
------------------------------------------ --------------- --------------- -------------- ---------------
(0 rows affected)
1>
2> drop FUNCTION DayOnly;