微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

MSSQLSERVER数据库- 存储过程

 写这篇存储过程的前参看了五六篇别人写的文章,看完后学到一些以前没有发现的东西,原来存储过程里有系统存储过程,原来存储过程还可以有返回值,我将把我从别人那里看到的,重新总结一下写出来。

      什么是存储过程

      如果你接触过其他的编程语言,那么就好理解了,存储过程就像是方法一样。竟然他是方法那么他就有类似的方法名,方法要传递的变量和返回结果,所以存储过程有存储过程名有存储过程参数也有返回值。 

存储过程的优点:    

  •       存储过程的能力大大增强了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
--可以看TABLE_OWNER字段显示DBO里确认是用户自己
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',
@filename2 = 'D:\Program Files\Microsoft sql Server\MSsql10.MSsqlSERVER\MSsql\DATA\test_log.ldf'

 

   用户自定义存储过程
      在创建一个存储过程前,先来说一下存储过程的命名,看到好几篇讲存储过程的文章都喜欢在创建存储过程的时候加一个前缀,养成在存储过程名前加前缀的习惯很重要,虽然这只是一件很小的事情,但是往往小细节决定大成败。看到有的人喜欢这样加前缀,例如proc_名字。也看到这加样前缀usp_名字。前一种proc是procedure的简写,后一种sup意思是user procedure。我比较喜欢第一种,那么下面所有的存储过程名都以第一种来写。至于名字的写法采用骆驼命名法。

创建存储过程的语法如下:

CREATE PROC[EDURE] 存储过程名 

@参数1 [数据类型]=[认值] [OUTPUT] 

@参数2 [数据类型]=[认值] [OUTPUT]

AS 

sql语句

EXEC 过程名[参数]

来看一下各种不同的存储过程的实例:

37
38
39
40
41
42
43
44
45
--创建不带参数的存储过程
create  procedure  pro_student
as
     select  * from  student;
--执行不带参数的存储过程
pro_student;
 
 
--修改不带参数的存储过程
alter  pro_student
as
student where  sid>3;
--执行修改后的存储过程
pro_student;
 
--删除存储过程
drop  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', --输出参数
@age int  output --输入输出参数
)
as
ssex = @sex and  sage = @age;
 
 
 
--不缓存在存储过程
use myschool;
proc_recompileStudent
with  recompile
as
student
     
proc_recompileStudent
 
--加密的存储过程
proc_encrptStudent
encryption
as
student;
     
proc_recompileStudent

  

存储过程返回值的方式

1、返回数字类型的存储过程(还没有想到返回字符串的方法

24
IF exists( sys.objects where  name = 'proc_getscore0' )
proc_getscore0
GO 
proc_getscore0
(
@id int
)
AS
BEGIN
declare  @score int
@score=english score id=@id
 
IF(@score>60)
         return  0
ELSE
1
END
 
--测试调用返回数字的存储过程<br>declare @t int
@t = proc_getscore0 2
@t;
 
--这里我遇到一个小问题,如果返回值是字符串,接收的时候declare @t nvarchar也出错,那该怎么做?
--暂时没有想到

  2、返回变量的存储过程

23
'proc_getscore' proc_getscore
GO
CREATE  PROCEDURE  proc_getscore
int 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
AS
BEGIN
int
id=@id
IF(@score>60)
set  @result= '及格'
ELSE   
'不及格'
 
END
GO
--测试一
int
@ temp  (50)
@id=3
proc_getscore @id,@ output
temp

        最后一个例子,用C#来调用具有返回值的存储过程,这里我通过调用返回变量类型的存储过程来做测试。测试在控件台下进行,以下写了两种方法,第二种更好,代码如下:

45
46
47
48
49
50
51
52
53
54
55
56
57
using  System;
System.Collections.Generic;
System.Linq;
System.Text;
System.Data;
System.Data.sqlClient;
namespace  ConsoleApplication1
{
     class  Program
     {
         static  void  Main( string [] args)
         {
             //方法
//using (sqlConnection conn = new sqlConnection("server=.;database=myschool;uid=sa;pwd=123456"))
//{
//    conn.open();
//    using (sqlCommand cmd = new sqlCommand("proc_getscore",conn))
//    {
//        cmd.CommandType = CommandType.StoredProcedure;
//        cmd.Parameters.AddWithValue("@id",2);
//        sqlParameter sp = cmd.Parameters.Add("@result",sqlDbType.VarChar,50);
//        sp.Direction = ParameterDirection.Output;
//        cmd.ExecuteNonQuery();
 
//       Console.Write(sp.Value);
//    }
//}
 
             
//方法
             (sqlConnection conn = new  sqlConnection( "server=.;database=myschool;uid=sa;pwd=123456" ))
             {
                 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[] paras = {
                         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),
"@result" 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;
paras[1].Direction = ParameterDirection.Output;
 
cmd.Parameters.AddRange(paras);
cmd.ExecuteNonQuery();
 
Console.Write(paras[1].Value);
}
}
 
Console.ReadLine();
 
 
}
}
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐