由于工作缘故,需要解决一个sql自定义函数,后来在网上查了很多才明白了其中的道理,我找到了自定义函数的语法格式,写法和调用方法
语法格式:CREATE FUNCTION <函数名称>
(
-- 函数的参数(可以多个)
<@param1, sysname, @p1>
写法(工作中的例子):CREATE FUNCTION getcallbackdata
(@mycallerno varchar(30),
@myid int
)
RETURNS @callbdata TABLE
(
callerarea varchar (10),
status int,
route int,
prefix varchar(20),
pcell int,
prate money,
prov varchar(20) NULL
)
AS
BEGIN
DECLARE @arecode varchar(10),
@status int,
@route int,
@prefix varchar(20),
@pcell int,
@prate money,
@prov varchar(20)
select @arecode=areacode from sd_areaplay
select @status=state from sd_account where pin='@mycallerno'
select @prefix=prefix from sd_platformrouter
select @pcell=feesec,@prate=afterfreefee from sd_payplan
insert into @callbdata values(@arecode,@status,@route,@prefix,@pcell,@prate,@prov);
RETURN
END
GO
调用方法:select callerarea,status,route,prefix,pcell,prate,prov from t.getcallbackdata('mycallerno',mypid)