SQL-存储过程

常用系统存储过程

 1 exec sp_databases; --查看数据库
 2 
 3 exec sp_tables;        --查看表
 4 
 5 exec sp_columns student;--查看列
 6 
 7 exec sp_helpIndex student;--查看索引
 8 
 9 exec sp_helpConstraint student;--约束
10 
11 exec sp_stored_procedures;
12 
13 exec sp_helptext 'sp_stored_procedures';--查看存储过程创建、定义语句
14 
15 exec sp_rename student, stuInfo;--修改表、索引、列的名称
16 --举例
17 --表重命名
18 exec sp_rename 'stu', 'stud';
19 --列重命名
20 exec sp_rename 'stud.name', 'sName', 'column';
21 --重命名索引
22 exec sp_rename N'student.idx_cid', N'idx_cidd', N'index';
23 
24 exec sp_renamedb myTempDB, myDB;--更改数据库名称
25 
26 exec sp_defaultdb 'master', 'myDB';--更改登录名的默认数据库
27 
28 exec sp_helpdb;--数据库帮助,查询数据库信息
29 --举例
30 exec sp_helpdb master;

查询所有存储过程

1 select * from sys.objects where type = 'P';
2 select * from sys.objects where type_desc like '%pro%' and name like 'sp%';

自定义存储过程

创建

带参数

1 create proc | procedure pro_name
2     [{@参数数据类型} [=默认值] [output],
3      {@参数数据类型} [=默认值] [output],
4      ....
5     ]
6 as
7     SQL_statements

各类参数:

⒈普通参数

1 create proc proc_find_stu(@startId int, @endId int)
2 as
3     select * from student where id between @startId and @endId
4 go
5 
6 exec proc_find_stu 2, 4;

⒉带通配符参数

1 create proc proc_findStudentByName(@name varchar(20) = '%j%', @nextName varchar(20) = '%')
2 as
3     select * from student where name like @name and name like @nextName;
4 go
5 
6 exec proc_findStudentByName;
7 exec proc_findStudentByName '%o%', 't%';

⒊带输出参数

 1 create proc proc_getStudentRecord(
 2     @id int, --默认输入参数
 3     @name varchar(20) out, --输出参数
 4     @age varchar(20) output--输入输出参数
 5 )
 6 as
 7     select @name = name, @age = age  from student where id = @id and sex = @age;
 8 go
 9 
10 -- 
11 declare @id int,
12         @name varchar(20),
13         @temp varchar(20);
14 set @id = 7; 
15 set @temp = 1;
16 exec proc_getStudentRecord @id, @name out, @temp output;
17 select @name, @temp;
18 print @name + '#' + @temp;

⒋带游标参数

create proc proc_cursor
    @cur cursor varying output
as
    set @cur = cursor forward_only static for
    select id, name, age from student;
    open @cur;
go
--调用
declare @exec_cur cursor;
declare @id int,
        @name varchar(20),
        @age int;
exec proc_cursor @cur = @exec_cur output;--调用存储过程
fetch next from @exec_cur into @id, @name, @age;
while (@@fetch_status = 0)
begin
    fetch next from @exec_cur into @id, @name, @age;
    print 'id: ' + convert(varchar, @id) + ', name: ' + @name + ', age: ' + convert(char, @age);
end
close @exec_cur;
deallocate @exec_cur;--删除游标

不带参数

1 --存储过程是否已存在
2 if (exists (select * from sys.objects where name = 'proc_get_student'))
3     drop proc proc_get_student
4 go
5 --创建不带参数存储过程
6 create proc proc_get_student
7 as
8     select * from student;

执行

1 exec proc_get_student;

修改

1 alter proc proc_get_student
2 as
3 select * from student;

不缓存存储过程

1 --WITH RECOMPILE 不缓存
2 create proc proc_temp
3 with recompile
4 as
5     select * from student;
6 go
7 
8 exec proc_temp;

加密存储过程

 1 --加密WITH ENCRYPTION 
 2 create proc proc_temp_encryption
 3 with encryption
 4 as
 5     select * from student;
 6 go
 7 
 8 exec proc_temp_encryption;
 9 exec sp_helptext 'proc_temp';
10 exec sp_helptext 'proc_temp_encryption';

Raiserror

Raiserror返回用户定义的错误信息,可以指定严重级别,设置系统变量记录所发生的错误。

语法

Raiserror({msg_id | msg_str | @local_variable}
  {, severity, state}
  [,argument[,…n]]
  [with option[,…n]]
)
-- msg_id : 在 sysmessages 系统表中指定的用户定义错误信息。

-- msg_str : 用户定义的信息,信息最大长度在 2047 个字符。

-- severity: 用户定义与该消息关联的严重级别。
---- 当使用 msg_id 引发使用 sp_addmessage 创建的用户定义消息时, raiserror 上指定严重性将覆盖 sp_addmessage 中定义的严重性。
---- 任何用户可以指定 0-18 直接的严重级别。
------ 只有 sysadmin 固定服务器角色常用或具有 alter trace 权限的用户才能指定 19-25 直接的严重级别。 19-25 之间的安全级别需要使用 with log 选项。

-- state:介于 1 至 127 直接的任何整数。State 默认值是 1 。

举例

raiserror('is error', 16, 1);
select * from sys.messages;
-- 使用 sysmessages 中定义的消息
raiserror(33003, 16, 1);
raiserror(33006, 16, 1);

 

posted @ 2021-09-14 09:56  Mra_m  阅读(49)  评论(0编辑  收藏  举报