码迷,mamicode.com
首页 > 数据库 > 详细

SQLServer存储过程

时间:2016-09-02 11:42:33      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:

--无参数存储过程创建和调用
--创建存储过程usp_test user go if exists(select * from sysobjects where name=‘usp_test‘) drop proc usp_test go create proc usp_test as select * from test go --执行存储过程 exec usp_test go
--有参存储过程创建和调用
--创建存储过程
user test
if exists (select * from sysobjects where name=‘usp_test‘)
drop proc usp_test
go
create proc usp_test
@name varchar(32),
@age int = null
as 
if @age is null
begin
 select * from test where name = @name
end
else 
begin
 select * from test where name = @name and age = @age
end
go

--调用存储过程
exec usp_test @name=‘username‘
go
    

  

--有参数、输出存储过程和调用
--统计某年龄的人数
--创建存储过程
use test
if exists (select * from sysobjects where name=‘usp_test‘)
drop proc usp_test
go
create proc usp_test
@age varchar(32),
@count int output
as 
set @count = (select count(1) from test where age = @age)
go

--执行存储过程
declare @age varchar(32)
declare @count int 
set @age = ‘26‘
exec usp_test @age ,@count output
print @count
go
 

  

SQLServer存储过程

标签:

原文地址:http://www.cnblogs.com/Lv2014/p/5832617.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!