• Sqlserver中存储过程,触发器,自定义函数(二)


    Sqlserver中存储过程,触发器,自定义函数:

    自定义函数:
    1.函数类型;
    2.函数的参数和返回值;

    1.函数类型:
    标量值函数,返回的是一个标量值
    表值函数:
    内联表值函数;
    多语句表值函数。

    标量值函数:

     1 go
     2  create function SumOrders(@职工号 varchar(20))--指定参数名,和返回类型  stuNo
     3   returns int --指定返回类型  
     4   begin  
     5    declare @订单总数 int  --学生人数sumstudent
     6    select @订单总数=count(订单.订单号) from  
     7     订单 join 职工 on 订单.职工号=职工.职工号  
     8     where 职工.职工号=@职工号  
     9    return @订单总数  
    10   end  
    11    
    12   select dbo.SumOrders('E4') 
    13    
    14  go 
    15   alter function SumStudent(@stuNo int)--指定参数名,和返回类型  stuNo
    16   returns int --指定返回类型  
    17   begin  
    18    declare @sumS int  --学生人数sumstudent
    19    select @sumS=count(*) from stuInfo   
    20     where stuNo>=@stuNo  
    21    return @sumS  
    22   end  
    23  go
    24   select dbo.SumStudent(4) as 总数

    --eg2:创建内联表值函数

     1 create function SelectOrdersByTime(@起始时间 datetime,@终止时间 datetime)  
     2 returns table  
     3 return select * from 订单 where  
     4  订单日期 between @起始时间 and @终止时间  
     5  
     6  go
     7 select * from SelectOrdersByTime('2003-01-01','2003-07-01')  
     8 --=======================
     9 go
    10 create function SelectinfoByTime(@起始时间 int,@终止时间 int)  
    11 returns table  
    12 return select * from stuInfo where  
    13  stuNo between @起始时间 and @终止时间  
    14  
    15  go
    16 select * from SelectinfoByTime(1,9)  

    --eg3:创建多语句表值函数

     1 create function MingDan()--无参函数  
     2      returns @名单 table  
     3      (  
     4        编号 int identity(1,1) not null,  
     5       姓名 nvarchar(10) not null  
     6      )  
     7       begin   
     8        insert @名单  
     9         select 供应商编号,姓名 from 供应商  
    10        insert @名单  
    11         select 职工编号,姓名 from 职工  
    12        return  
    13       end  
    14     go   
    15      select * from MingDan()  
    16      
    17  --eg4:自定义函数生成默认值:
    18  go
    19  create function Default_Num()
    20     returns varchar(7)
    21     begin
    22         declare @编号 varchar(7)
    23         declare @id int
    24         select top 1 @编号=编号 from testorder by 编号 desc
    25         if @@rowcount=0
    26             set @编号='TCP_001'
    27         else
    28         begin
    29             set @id=cast(substring(@编号,5,3)asint) + 1
    30             set @编号='TCP_' + replicate('0',3-len(@id)) +cast(@idasvarchar(3))
    31         end
    32         return @编号
    33     end
     --eg4:自定义函数生成默认值:
     1  create function Default_Num()
     2     returns varchar(7)
     3     begin
     4         declare @编号 varchar(7)
     5         declare @id int
     6         select top 1 @编号=编号 from testorder by 编号 desc
     7         if @@rowcount=0
     8             set @编号='TCP_001'
     9         else
    10         begin
    11             set @id=cast(substring(@编号,5,3)asint) + 1
    12             set @编号='TCP_' + replicate('0',3-len(@id)) +cast(@idasvarchar(3))
    13         end
    14         return @编号
    15     end
  • 相关阅读:
    业务对象(BO)设计
    业务对象和BAPI
    LSMW应用
    BDC、CATT批量数据维护
    ABAP RFC远程调用
    LIST动态表格画线(动态列)
    ALV详解:OO SALV
    ALV详解:OO ALV
    ALV详解:Function ALV(二)
    ALV详解:Function ALV(一)
  • 原文地址:https://www.cnblogs.com/linjulin/p/5620896.html
Copyright © 2020-2023  润新知