Sql存储过程

下面的存储过程从四个表的联接中返回所有作者(提供了姓名)、出版的书籍以及出版社。该存储过程不使用任何参数。 

USE pubs  
IF EXISTS (SELECT name FROM sysobjects  
          WHERE name = 'au_info_all' AND type = 'P')    DROP PROCEDURE au_info_all 

GO 

CREATE PROCEDURE au_info_all 

AS  
SELECT au_lname, au_fname, title, pub_name    FROM authors a INNER JOIN titleauthor ta       ON a.au_id = ta.au_id INNER JOIN titles t  
       ON t.title_id = ta.title_id INNER JOIN publishers p       ON t.pub_id = p.pub_id

GO 

EXECUTE au_info_all
 


使用带有参数的简单过程 

CREATE PROCEDURE au_info    @lastname varchar(40),    @firstname varchar(20) AS  
SELECT au_lname, au_fname, title, pub_name    FROM authors a INNER JOIN titleauthor ta       ON a.au_id = ta.au_id INNER JOIN titles t  
       ON t.title_id = ta.title_id INNER JOIN publishers p       ON t.pub_id = p.pub_id    WHERE  au_fname = @firstname       AND au_lname = @lastname GO  
   au_info

 

存储过程可以通过以下方法执行:

EXECUTE au_info 'Dull', 'Ann' 
posted @ 2015-02-11 09:46  kaleozhou  阅读(134)  评论(0编辑  收藏  举报