存储过程几种写法

1)创建使用参数的存储过程

Create Proc au_info @lastname varchar(40),@firstname varchar(20)

As

Select  au_lname,au_fname,title,pub_name

From ...

where au_fname=@firstname And au_lname=@lastname

Go

EXECUTE  au_info  ringer,anne

2)创建使用参数默认值的存储过程,该存储过程在没有输入参数的情况下将默认值得到的结果输出

Create Proc au_info @lastname varchar(40)='ringer',@firstname varchar(20)='anne'

As

Select  au_lname,au_fname,title,pub_name

From ...

where au_fname=@firstname And au_lname=@lastname

Go

EXECUTE  au_info

 3)用显式值替代参数默认值的存储过程

Create Proc showind @table varchar(30) ='titles'

as

SELECT Table_Name=sysobjects.name,

INDEX_Name=sysindexes.name,index_id=indid

from sysindexes inner join sysobjects on sysobjects.id=sysindexes.id

where sysobjects.name=@table

EXECUTE showind authors

4)使用参数默认值NULL来创建存储过程,在这种情况下如果没有提供参数值,SQL将不会出错显示

Create Proc showind @table varchar(30) =Null

as

IF @table is NUll

print '请输入参数'

else

SELECT Table_Name=sysobjects.name,

INDEX_Name=sysindexes.name,index_id=indid

from sysindexs inner join sysobjects on sysobjects.id=sysindexes.id

where sysobjects.name=@table

EXECUTE showind authors

5)使用包含通配符的参数默认值创建存储过程

通配符包括(% , _ , [ ]和 [^]),注意需要用Like关键字

CREATE PROC au_info @lastname varchar(40)='r%' , @firstname varchar(20)='%'  AS

Select au_lname,au_fname,title,pub_name
from authors inner join titleauthor on authors.au_id=titleauthor.au_id
join titles on titleauthor.title_id=titles.title_id
join publishers on titles.pub_id=publishers.pub_id

where au_fname like @firstname
and au_lname like @lastname
GO

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页