写这篇存储过程的前参看了五六篇别人写的文章,看完后学到一些以前没有发现的东西,原来存储过程里有系统存储过程,原来存储过程还可以有返回值,我将把我从别人那里看到的,重新总结一下写出来。
什么是存储过程
如果你接触过其他的编程语言,那么就好理解了,存储过程就像是方法一样。竟然他是方法那么他就有类似的方法名,方法要传递的变量和返回结果,所以存储过程有存储过程名有存储过程参数也有返回值。
存储过程的优点:
- 存储过程的能力大大增强了sql语言的功能和灵活性。
- 可保证数据的安全性和完整性。
- 通过存储过程可以使没有权限的用户在控制之下间接地存取数据库,从而保证数据的安全。
- 通过存储过程可以使相关的动作在一起发生,从而可以维护数据库的完整性。
- 在运行存储过程前,数据库已对其进行了语法和句法分析,并给出了优化执行方案。这种已经编译好的过程可极大地改善sql语句的性能。
- 可以降低网络的通信量。
- 使体现企业规则的运算程序放入数据库服务器中,以便 集中控制。
存储过程可以分为系统存储过程、扩展存储过程和用户自定义的存储过程
系统存储过程
我们先来看一下系统存储过程,系统存储过程由系统定义,主要存放在MASTER数据库中,名称以"SP"开头或以"XP"开头。尽管这些系统存储过程在MASTER数据库中,但我们在其他数据库还是可以调用系统存储过程。有一些系统存储过程会在创建新的数据库的时候被自动创建在当前数据库中。
常用系统存储过程有:
- exec sp_databases; --查看数据库
- exec sp_tables; --查看表
- exec sp_columns student;--查看列
- exec sp_helpIndex student;--查看索引
- exec sp_helpConstraint student;--约束
- exec sp_helptext 'sp_stored_procedures';--查看存储过程创建定义的语句
- exec sp_stored_procedures;
- exec sp_rename student,stuInfo;--更改表名
- exec sp_renamedb myTempDB,myDB;--更改数据库名称
- exec sp_defaultdb 'master','myDB';--更改登录名的默认数据库
- exec sp_helpdb;--数据库帮助,查询数据库信息
- exec sp_helpdb master;
- exec sp_attach_db --附加数据库
- exec sp_detach_db --分离数据库
来看一下具体的代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
exec
sp_databases
--查看有哪些数据库
use MySchool
sp_tables
sp_columns student
--除了用系统视图可以查看列,用系统存储过程也可以查看到列
sp_helpindex student
--查看索引,可以看到索引的描述,经过测试发现主键也是索种的一种
sp_helpconstraint student
--查看约束
sp_helptext
'sys.all_columns'
--查看系统视图
'sp_test'
sp_stored_procedures
--查看全部的存储过程
sp_rename
'student'
,
'stuInfo'
--更改表名
use master
sp_renamedb
'myschool'
'school'
sp_rename N
'student.idx_cid'
sql plain" style="margin:0px!important; padding:0px!important; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:2em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:'Courier New',N
'idx_cidd'
'index'
;
--重命名索引
sp_helpdb
--分离数据库
use myschool
sp_detach_db
'test'
;
--exec sp_attach_db --附加数据库
EXEC
sp_attach_db @dbname =
sql string" style="margin:0px!important; padding:0px!important; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:2em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:'Courier New',
@filename1 =
'D:\Program Files\Microsoft sql Server\MSsql10.MSsqlSERVER\MSsql\DATA\test.mdf'
sql plain" style="margin:0px!important; padding:0px!important; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:2em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:'Courier New',
|
用户自定义存储过程
在创建一个存储过程前,先来说一下存储过程的命名,看到好几篇讲存储过程的文章都喜欢在创建存储过程的时候加一个前缀,养成在存储过程名前加前缀的习惯很重要,虽然这只是一件很小的事情,但是往往小细节决定大成败。看到有的人喜欢这样加前缀,例如proc_名字。也看到这加样前缀usp_名字。前一种proc是procedure的简写,后一种sup意思是user procedure。我比较喜欢第一种,那么下面所有的存储过程名都以第一种来写。至于名字的写法采用骆驼命名法。
创建存储过程的语法如下:
CREATE PROC[EDURE] 存储过程名
@参数1 [数据类型]=[默认值] [OUTPUT]
@参数2 [数据类型]=[默认值] [OUTPUT]
AS
sql语句
EXEC 过程名[参数]
来看一下各种不同的存储过程的实例:
create
procedure
pro_student
select
*
from
student;
pro_student;
alter
pro_student
student
where
sid>3;
pro_student;
--删除存储过程
pro_student;
create
proc proc_getStudentRecord
@sex
varchar
(2)
out
sql plain" style="margin:0px!important; padding:0px!important; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:2em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:'Courier New',
--输出参数
)
ssex = @sex
and
sage = @age;
--不缓存在存储过程
proc_recompileStudent
recompile
as
proc_encrptStudent
as
存储过程返回值的方式
1、返回数字类型的存储过程(还没有想到返回字符串的方法)
2、返回变量的存储过程
proc_getscore
sql plain" style="margin:0px!important; padding:0px!important; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:2em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:'Courier New',
@result
(50)
output
BEGIN
id=@id
set
@result=
'及格'
'不及格'
END
--测试一
@
temp
(50)
proc_getscore @id,@
output
最后一个例子,用C#来调用具有返回值的存储过程,这里我通过调用返回变量类型的存储过程来做测试。测试在控件台下进行,以下写了两种方法,第二种更好,代码如下:
using
System;
System.Collections.Generic;
System.Text;
ConsoleApplication1
{
class
Program
{
static
void
Main(
string
[] args)
{
//方法一
// conn.open();
// {
// cmd.Parameters.AddWithValue("@id",2);
// sp.Direction = ParameterDirection.Output;
// }
//方法二
{
conn.open();
(sqlCommand cmd =
sqlCommand(
"proc_getscore"
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:2em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:'Courier New',conn))
cmd.CommandType = CommandType.StoredProcedure;
sqlParameter(
"@id"
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:2em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:'Courier New',sqlDbType.Int),
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:2em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:'Courier New',sqlDbType.NVarChar,50)
};
paras[0].Value = 2;
cmd.ExecuteNonQuery();
Console.Write(paras[1].Value);
}
Console.ReadLine();
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。