• 存储过程


    --无参数无返回值
    create proc pl
    as
    begin
    select*from Student
    end
    exec pl
    drop proc pl
    --有参数无返回值
    create proc p2
    @banj_id char(10), @Ssex char(2)
    as
    begin
    select*from Student where Class=@banj_id and Ssex=@Ssex 
    end
    drop proc p2
    exec p2 95031,男
    --默认参数无返回值
    create proc p3
    @Ssex char(2)=as
    select*from Student where Ssex like @Ssex+'%'
    exec p3 
    --有返回值无参数
    create proc p4
    @Sno char(10) output
    as 
    begin
    select @Sno=Sname from Student where Sno=105
    end
    declare @rt char(10)
    exec p4 @rt output
    print @rt
    --有返回值有参数
    create proc p5
    @a int,@b int,
    @c int output
    as
    begin
    set @c=@a+@b
    end
    declare @rt int
    exec p5 1,2,@rt output
    print @rt
    --返回值return写法,只能返回单个数据值
    alter proc p5
    @a int,@b int
    as
    begin
    return @a+@b
    end
    declare @rt int
    exec @rt=p5 1,2
    print @rt
    
    --动态查询
    --解析执行语句
    exec('select*form Student')
    
    create proc p6
    @tablename varchar(15)
    as
    begin
    exec('select*from '+@tablename)
    end
    drop proc p6
    exec p6 'Student'
    
    create proc p7
    @tablename char(10),@column char(10),@value char(10)
    as
    declare @query varchar(255)
    select @query='select*from '+@tablename+' where '+@colum=@value
    exec (@query)
    exec p7 student,class,95033
    --标量函数返回唯一数据值
    create function f1 ()
    returns int
    as
    begin
    return 1
    end
    select dbo.f1()
    
    create function f2
    (@a int ,@b int)
    returns int
    as 
    begin
    return @a+@b
    end
    declare @rt int
    select @rt=dbo.f2(1,2)
    print @rt
    --内嵌表值函数
    create function f3
    ()
    returns table
    as
    return(select *from student) 
    select*from dbo.f3()
    --多语句表值函数
    alter function f4
    ()
    returns @t table
    (sno int,sname varchar(20))
    as
    begin
    insert into @t select top 2 sno,sname from student
    return
    end
    select*from dbo.f4()
  • 相关阅读:
    hdu 5335 Walk Out (搜索)
    Hdu 5336 XYZ and Drops (bfs 模拟)
    Zznu 1913: yifan and matrix (多路归并)
    hdu 5316 Magician (线段树)
    Bzoj 2038: [2009国家集训队]小Z的袜子(hose)
    Poj 1741 Tree (树的分治)
    LightOJ 1027
    1067
    Closest Common Ancestors---poj1470(LCA+离线算法)
    1128
  • 原文地址:https://www.cnblogs.com/liuyudong0825/p/4764505.html
Copyright © 2020-2023  润新知