--无参数无返回值create proc plasbeginselect*from Studentendexec pldrop proc pl
--有参数无返回值create proc p2@banj_id char(10), @Ssex char(2)asbeginselect*from Student where Class=@banj_id and Ssex=@Ssex enddrop proc p2exec p2 95031,男
--默认参数无返回值create proc p3@Ssex char(2)=男asselect*from Student where Ssex like @Ssex+'%'exec p3
--有返回值无参数create proc p4@Sno char(10) outputas beginselect @Sno=Sname from Student where Sno=105enddeclare @rt char(10)exec p4 @rt outputprint @rt
--有返回值有参数create proc p5@a int,@b int,@c int outputasbeginset @c=@a+@benddeclare @rt intexec p5 1,2,@rt outputprint @rt
--返回值return写法,只能返回单个数据值alter proc p5@a int,@b intasbeginreturn @a+@benddeclare @rt intexec @rt=p5 1,2print @rt
--动态查询--解析执行语句exec('select*form Student')create proc p6@tablename varchar(15)asbeginexec('select*from '+@tablename)enddrop proc p6exec p6 'Student'create proc p7@tablename char(10),@column char(10),@value char(10)asdeclare @query varchar(255)select @query='select*from '+@tablename+' where '+@colum=@valueexec (@query)exec p7 student,class,95033
--标量函数返回唯一数据值create function f1 ()returns intasbeginreturn 1endselect dbo.f1()create function f2(@a int ,@b int)returns intas beginreturn @a+@benddeclare @rt intselect @rt=dbo.f2(1,2)print @rt
--内嵌表值函数create function f3()returns tableasreturn(select *from student) select*from dbo.f3()--多语句表值函数alter function f4()returns @t table(sno int,sname varchar(20))asbegininsert into @t select top 2 sno,sname from studentreturnendselect*from dbo.f4()