1.创建一个存储过程

create procedure up_search_contact1
@s int
as
  select @s= count(distinct is_noncriminal) from contact
go 

如果是临时存储过程,可以在up_search_contact1加#(本地临时存储过程),##(全局临时存储过程),关闭后,这些过程将不复存在。

eg:

create procedure #up_search_contact1

2.执行存储过程

declare  @s int,@returnval int
exec @returnval= up_search_contact1
select@s=@s output --注意这里必须有select @s=@s output ,否则将会出错,他不知道想要出查什么,因为默认会有返回值

eg:

 declare  @s int,@returnVal int
exec @returnVal= up_search_contact1
@s=@s output

(1)修改存储过程

 

alter procedure #up_search_contact
 @s int
as
select * from contact where is_noncriminal=@s
go

declare @s int
 exec #up_search_contact @S=1--这个可以直接显示所要向要执行的sql语句的值 select * from contact where is_noncriminal=@s

(2)相对复杂的存储过程

create procedure #test
as
create table #tb_test(id int primary key)

declare @count1 int
set @count1=31
while @count1<50--利用这种方法循环赋值,但是效率有点慢 ,
begin
insert into #tb_test values(@count1)
set @count1=@count1+1
end

select * from #tb_test order by id desc;
go

exec #test--执行存储过程

有三种方法可以执行一个存储过程

i〉exec #test

ii〉execute #test
iii〉#test

 

alter procedure #test
as
create table #tb_test(id int primary key)
 
insert into #tb_test select distinct contact_id from contact where is_noncriminal=1--第二种赋值的方法,这种方法效率相当高,而且简单通过select一个表
 

select * from #tb_test order by id desc;
go


 


 

Posted on 2008-10-23 16:16  sunlibo  阅读(124)  评论(0)    收藏  举报