存储过程相当于C# Java的函数
输入参数就是你要传入的一些值 。它从外面进来帮助你完成你的存储过程。
输出参数就是你要传出去的一些值。它是从存储过程里面得出的值来帮助传出去完成外面要做的事情。
区别就是一个进一个出。输入参数是外程序帮助存储过程;输出参数是存储过程帮助外程序
1. 没有参数输入输出
2. 有参数输入,无参数输出
3. 参数输入,参数输出
在调用的时候,需要在外部单独声明一个变量,exec 输入参数,输出参数 output
写这么多,是为自己今后学习用。
1, 存储过程(Procedure)-基本创建与操作。 |
--================================
-- ylb:存储过程创建与操作
--================================
use pubs
go
--一、无参存储过程
--1,创建存储过程
create
procedure
PTitles
as
select
*
from
titles
go
--2,执行存储过程
execute
PTitles
go
--3,移除存储过程
--drop procedure PTitles
go
|
2, 存储过程(Procedure)-带入参 |
--==============================
-- ylb:存储过程-入参
-- 16:44 2011/12/14
--==============================
use pubs
go
--1,创建带入参存储过程
select
*
from
titles
where
type=
'business'
go
create
proc P_Titles_ByType
@type
char
(12)
--入参
as
select
*
from
titles
where
type=@type
go
--2,执行带参数的存储过程
--a)方式一
exec
P_Titles_ByType @type=
'business'
go
--b)方式二
exec
P_Titles_ByType
'business'
go
--P1:写一个存储过程,要求图书类型是business且单价大于10的所有信息
--P1_1,创建存储过程
select
*
from
titles
where
type=
'business'
and
price>10
go
create
proc P_Titles_ByTypeAndPrice
@type
char
(12),
--入参
@price money
--入参
as
select
*
from
titles
where
type=@type
and
price>@price
go
--P1_2,执行存储过程
exec
P_Titles_ByTypeAndPrice
@type=
'business'
,@price=10
go
exec
P_Titles_ByTypeAndPrice
@price=10,@type=
'business'
go
exec
P_Titles_ByTypeAndPrice
'business'
,10
go
--是错的,当你直接给值时,一定注意参数的顺序和类型。
--exec P_Titles_ByTypeAndPrice 10,'business'
|
3, 存储过程(Procedure)-带入参和出参。 |
--================================
-- ylb:存储过程-带入参和出参
-- 16:44 2011/12/14
--================================
use pubs
go
select
*
from
titles
--P1:查图书编号是“BU1032”的图书的单价是多少?
select
price
from
titles
where
title_id=
'BU1032'
go
--P1_1,创建
create
proc P_Titles_ByTitleID_SelectPrice
@title_id
varchar
(6)
--入参
as
select
price
from
titles
where
title_id=@title_id
go
--P1_2,执行
exec
P_Titles_ByTitleID_SelectPrice
'BU1032'
go
--P2_1,创建
create
proc P_Titles_ByTitleID_SelectPrice2
@title_id
varchar
(6),
--入参
@price money
output
--出参【出参家标识(output)】
as
select
@price=price
from
titles
where
title_id=@title_id
--出参的@在=左边
go
--1,先声明变量
declare
@price2 money
--2,之后在调用
exec
P_Titles_ByTitleID_SelectPrice2
@title_id=
'BU1032'
,
@price=@price2
output
--3,再之后在查声明变量
select
@price2
--出参要声明,配参后面要加output标识,之后再查声明变量。
|
CREATE PROCEDURE dbo.my_proc
@first int = NULL, -- NULL default value
@second int = 2, -- Default value of 2
@third int = 3 -- Default value of 3
AS
SET NOCOUNT ON;
SELECT @first, @second, @third;
GO
@first int = NULL, -- NULL default value
@second int = 2, -- Default value of 2
@third int = 3 -- Default value of 3
AS
SET NOCOUNT ON;
SELECT @first, @second, @third;
GO
http://www.cnblogs.com/ylbtech/archive/2012/08/14/2638257.html#ylb3