Heim  >  Artikel  >  Datenbank  >  怎么创建存储过程

怎么创建存储过程

WBOY
WBOYOriginal
2016-06-07 17:46:381314Durchsuche

这篇文章主要是讲使用存储过程创建表怎么创建存储过程以及如何创建存储过程为主题的文章

表book的内容如下

   编号    书名                           价格 

   001      C语言入门                   $30

   002      PowerBuilder报表开发  $52 

 实例1:查询表Book的内容的存储过程 

以下为引用的内容:

   create proc query_book

 

      as

 

      select * from book

 

   go 

   exec query_book

 


 实例2:加入一笔记录到表book,并查询此表中所有书籍的总金额

 

以下为引用的内容:

   Create proc insert_book


   @param1 char(10),@param2 varchar(20),@param3 money,@param4 money output


   with encryption  ---------加密

   as

   insert book(编号,书名,价格) Values(@param1,@param2,@param3)
   select @param4=sum(价格) from book
  go
 


  执行例子:


以下为引用的内容:
  declare @total_price money
  exec insert_book '003','Delphi 控件开发指南',$100,@total_price
  print '总金额为'+convert(varchar,@total_price)
  go


存储过程的3种传回值:
   1.以Return传回整数
   2.以output格式传回参数
   3.Recordset 

传回值的区别:
       output和return都可在批次程式中用变量接收,而recordset则传回到执行批次的客户端中  

实例3:设有两个表为Product,Order,其表内容如下:

以下为引用的内容:
      Product
           产品编号       产品名称    客户订数    
            001             钢笔         30        
            002             毛笔         50         
            003             铅笔         100       
      order 
           产品编号         客户名     客户订金
            001              南山区      $30
            002              罗湖区      $50
            003              宝安区      $4


请实现按编号为连接条件,将两个表连接成一个临时表,该表只含编号.产品名.客户名.订金.总金额,
总金额=订金*订数,临时表放在存储过程中

代码如下: 

以下为引用的内容:
     Create proc temp_sale
     as
       select a.产品编号,a.产品名称,b.客户名,b.客户订金,a.客户订数* b.客户订金 as总金额
       into #temptable from Product a inner join order b on a.产品编号=b.产品编号
    if  @@error=0
       print 'Good'
    else
       print 'Fail'
     go

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn