当前位置:Gxlcms > 数据库问题 > Sqlserver中存储过程,触发器,自定义函数(一)

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

时间:2021-07-01 10:21:17 帮助过:2人阅读

create proc CountOfOrders--指定存储过程名 2 as--指定存储过程的主体 3 begin 4 declare @CountOfOrders as int--声明一个作为int类型的存储过程的内部变量 5 select @CountOfOrders = Count(*) fromt orders--将sql语句的返回值赋给前面定义的变量 6 print convert(verchar(10),@CountOfOrders)--将变量转换为字符串型打印 7 end 8 go--确定一个执行计划 9 exec CountOfOrders--执行过程

以stuinfo表为例子:

1 create     proc countofinfoq
2         as
3         begin
4             declare @CountOfOrders as int--声明一个作为int类型的存储过程的内部变量  
5             select @CountOfOrders = Count(*) from stuDB.dbo.stuInfo--将sql语句的返回值赋给前面定义的变量  
6             --print convert(varchar(10),@CountOfOrders)--将变量转换为字符串型打印  
7             print @CountOfOrders
8         end
9         exec countofinfoq

eg2:查询任意数据库表的记录个数,这里需要指定参数,要注意参数的定义和执行的时的参数传递

 1 create proc CountOfTable  
 2    @TableName as Varchar(20)--定义一个普通的参数  
 3    as  
 4     begin  
 5      declare @Count as int  
 6      exec(select * into tmptable from  + @TableName)--参数的使用方法,这里exec相当于调用一个新的存储过程  
 7      select @Count=Count(*) from tmptable--用临时表缓存原表的数据,对临时表操作完后,删除临时表  
 8      drop table tmptable  
 9      return @Count--存储过程的返回值,只能是整数值!!!  
10     end  
11    declare @Count as int   --声明一个变量接收返回值  
12    exec @Count=CountOfTable 仓库    
13    print @Count  

以stuinfo表为例子:

技术分享
 1  select * from stuinfo
 2  drop table stuinfobak1
 3  select * into stuDB.dbo.stuinfobak1 from stuinfo
 4  go
 5  create proc CountOfTable1 
 6    @TableName as Varchar(20)--定义一个普通的参数  
 7    as  
 8     begin  
 9      declare @Count as int  
10      exec(select * into stuDB.dbo.stuinfobak2 from  + @TableName)--参数的使用方法,这里exec相当于调用一个新的存储过程  
11      select @Count=Count(*) from stuDB.dbo.stuinfobak2--用临时表缓存原表的数据,对临时表操作完后,删除临时表  
12      drop table stuinfobak2  
13      return @Count--存储过程的返回值,只能是整数值!!!  
14     end  
15   go
16    declare @Count as int   --声明一个变量接收返回值  
17    exec @Count=CountOfTable1     stuinfo
18    print @Count
View Code

调用:

1 declare @Count as int   --声明一个变量接收返回值  
2    declare @Table as varchar(20)  
3    set @Table = 仓库  
4    exec @Count=CountOfTable @Table  
5    print @Count  

eg3:参数传递方式:

 1 create proc ParamsTransfer  
 2       @类别名称 varchar(15),  
 3       @单价 money=$10,  
 4       @库存量 smallint,  
 5       @订购量 smallint = 5--带默认值,假如没有给它传值,则使用默认值  
 6      as  
 7       begin  
 8        select * from 产品  
 9         join 类别 on 产品.id = 类别.id  
10         where  
11          类别.类别名称=@类别名称 and   
12          产品.单价 > @单价 and  
13          产品.库存量 > @库存量 and  
14          产品.订购量 > @订购量  
15        end  
16       exec ParamsTransfer 饮料,1,10,20--顺序传值  
17       exec ParamsTransfer @单价=1,@订购量=20,@库存量=10,@类别名称=饮料--不按顺序传值  
18       exec ParamsTransfer 饮料,default,10,default--使用默认值  
19       exec ParamsTransfer 饮料,default,10--不指定default也是使用默认值  
20       exec ParamsTransfer @类别名称=饮料,@库存量=10--不按顺序并且使用默认值的传值

--eg4:存储过程的返回值: return一个整数值;使用output参数;返回结构集。

 1 select * from stuinfo
 2   select * from stuinfobak1
 3  drop table stuinfobak1
 4  select * into stuDB.dbo.stuinfobak1 from stuinfo
 5 go
 6     create proc ReturnValue  
 7    @返回值1 varchar(20) output  
 8   as  
 9    begin   
10     declare @返回值2 int  
11     declare @stuID库数 int  
12     select @stuID库数=Count(distinct stuID) from stuinfo  
13     
14     set @返回值1 = ‘‘ + cast(@stuID库数 as varchar(10))  
15     select @返回值2=Count(distinct stuID) from stuinfo  
16     return @返回值2  
17   end  
18   go  
19   declare @接收值1 varchar(20)    
20   declare @接收值2 int  
21   exec @接收值2=ReturnValue @接收值1 output  
22   print @接收值1  
23   print @接收值2  
24 
25 go
26 alter proc ReturnValue  
27    @返回值1 varchar(20) output  
28   as  
29    begin   
30     declare @返回值2 int  
31     declare @stuID库数 int  
32     select @stuID库数=Count(distinct stuID) from stuinfo  
33     
34     set @返回值1 = ---- + cast(@stuID库数 as varchar(10))  
35     select @返回值2=Count(distinct stuID) from stuinfo  
36     return @返回值2 
37   end  
38   go  
39   declare @接收值1 varchar(20)    
40   declare @接收值2 int  
41   exec @接收值2=ReturnValue @接收值1 output  
42   print @接收值1  
43   print @接收值2  

--eg5:调用存储过程返回一个打开的游标,这个是存储过程跟游标的小结合 ----------------------还是看不懂。。

 1 create proc UseCursor  
 2     @cursor cursor Varying output  
 3    as  
 4     begin  
 5      set @cursor=Cursor forward_only static for  
 6      select top 10 * from 订单  
 7      open @cursor  
 8     end  
 9    declare @my_cursor cursor   
10    declare @订单号 varchar(20)  
11    declare @供应商号 varchar(20)  
12    declare @职工号 varchar(20)  
13    declare @订单日期 varchar(30)  
14    exec UseCursor @my_cursor output  
15    fetch next from @my_cursor   
16     into @职工号,@订单号,@供应商号,@订单日期  
17     while @@fetch_status=0  
18      begin   
19       print @订单号 +  --  + @订单日期  
20       fetch next from @my_cursor   
21        into @职工号,@订单号,@供应商号,@订单日期  
22      end  

 

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

标签:

人气教程排行