在SQL中存储过程的一般语法
一般分为十种情况,每种语法各不相同:
1、 创建语法
|
1
2
3
4
5
6
7
|
create proc | procedure pro_name [{@参数数据类型} [=默认值] [output], {@参数数据类型} [=默认值] [output], .... ]as SQL_statements |
2、 创建不带参数存储过程
|
1
2
3
4
5
6
7
8
9
10
|
--创建存储过程if (exists (select * from sys.objects where name = 'proc_get_student')) drop proc proc_get_studentgocreate proc proc_get_studentas select * from student;--调用、执行存储过程exec proc_get_student; |
3、 修改存储过程
|
1
2
3
4
|
--修改存储过程alter proc proc_get_studentasselect * from student; |
4、 带参存储过程
|
1
2
3
4
5
6
7
8
9
10
|
--带参存储过程if (object_id('proc_find_stu', 'P') is not null) drop proc proc_find_stugocreate proc proc_find_stu(@startId int, @endId int)as select * from student where id between @startId and @endIdgoexec proc_find_stu 2, 4; |
5、 带通配符参数存储过程
|
1
2
3
4
5
6
7
8
9
10
11
|
--带通配符参数存储过程if (object_id('proc_findStudentByName', 'P') is not null) drop proc proc_findStudentByNamegocreate proc proc_findStudentByName(@name varchar(20) = '%j%', @nextName varchar(20) = '%')as select * from student where name like @name and name like @nextName;goexec proc_findStudentByName;exec proc_findStudentByName '%o%', 't%'; |
6、 带输出参数存储过程
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
if (object_id('proc_getStudentRecord', 'P') is not null) drop proc proc_getStudentRecordgocreate proc proc_getStudentRecord( @id int, --默认输入参数 @name varchar(20) out, --输出参数 @age varchar(20) output--输入输出参数)as select @name = name, @age = age from student where id = @id and sex = @age;go-- declare @id int, @name varchar(20), @temp varchar(20);set @id = 7; set @temp = 1;exec proc_getStudentRecord @id, @name out, @temp output;select @name, @temp;print @name + '#' + @temp; |
7、 不缓存存储过程
|
1
2
3
4
5
6
7
8
9
10
11
|
--WITH RECOMPILE 不缓存if (object_id('proc_temp', 'P') is not null) drop proc proc_tempgocreate proc proc_tempwith recompileas select * from student;goexec proc_temp; |
8、 加密存储过程
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
--加密WITH ENCRYPTION if (object_id('proc_temp_encryption', 'P') is not null) drop proc proc_temp_encryptiongocreate proc proc_temp_encryptionwith encryptionas select * from student;goexec proc_temp_encryption;exec <a href="https://www.baidu.com/s?wd=sp_helptext&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1YYm103n1DYmHfknhD3nWD10ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EPW63nHcdrH6" target="_blank" class="baidu-highlight">sp_helptext</a> 'proc_temp';exec <a href="https://www.baidu.com/s?wd=sp_helptext&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1YYm103n1DYmHfknhD3nWD10ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EPW63nHcdrH6" target="_blank" class="baidu-highlight">sp_helptext</a> 'proc_temp_encryption'; |
9、 带游标参数存储过程
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
if (object_id('proc_cursor', 'P') is not null) drop proc proc_cursorgocreate proc proc_cursor @cur cursor varying outputas 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 (<a href="https://www.baidu.com/s?wd=%40%40fetch_status&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1YYm103n1DYmHfknhD3nWD10ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EPW63nHcdrH6" target="_blank" class="baidu-highlight">@@fetch_status</a> = 0)begin fetch next from @exec_cur into @id, @name, @age; print 'id: ' + convert(varchar, @id) + ', name: ' + @name + ', age: ' + convert(char, @age);endclose @exec_cur;deallocate @exec_cur;--删除游标 |
10、 分页存储过程
|
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
|
---存储过程、row_number完成分页if (object_id('pro_page', 'P') is not null) drop proc proc_cursorgocreate proc pro_page @startIndex int, @endIndex intas select count(*) from product; select * from ( select row_number() over(order by pid) as rowId, * from product ) temp where temp.rowId between @startIndex and @endIndexgo--drop proc pro_pageexec pro_page 1, 4----分页存储过程if (object_id('pro_page', 'P') is not null) drop proc pro_stugocreate procedure pro_stu( @pageIndex int, @pageSize int)as declare @startRow int, @endRow int set @startRow = (@pageIndex - 1) * @pageSize +1 set @endRow = @startRow + @pageSize -1 select * from ( select *, row_number() over (order by id asc) as number from student ) t where t.number between @startRow and @endRow;exec pro_stu 2, 2; |
在SQL中存储过程的一般语法的更多相关文章
- SQL中存储过程和函数的区别
转:https://www.cnblogs.com/jacketlin/p/7874009.html 本质上没区别.只是函数有如:只能返回一个变量的限制.而存储过程可以返回多个. 而函数是可以嵌入在s ...
- 面试问题 - SQL 中存储过程与函数的区别
SQL 中的存储过程与函数没有本质上的区别 函数 -> 只能返回一个变量. 函数可以嵌入到sql中使用, 可以在select 中调用, 而存储过程不行. 但函数也有着更多的限制,比如不能使用临 ...
- Sql 中存储过程详细案例
转自:http://www.cnblogs.com/yank/p/4235609.html 概念 存储过程(Stored Procedure):已预编译为一个可执行过程的一个或多个SQL语句. 创建存 ...
- SQL中存储过程和自定义函数的区别
存储过程: 存储过程可以使得对数据库的管理.以及显示关于数据库及其用户信息的工作容易得多.存储过程是 SQL 语句和可选控制流语句的预编译集合,以一个名称存储并作为一个单元处理.存储过程存储在 ...
- SQL中存储过程的例子
导读:sql存储是数据库操作过程中比较重要的一个环节,对于一些初学者来说也是比较抽象难理解的,本文我将通过几个实例来解析数据库中的sql存储过程,这样就将抽象的事物形象化,比较容易理解. 例1: cr ...
- SQL中存储过程中使用事务,并且加入异常处理机制.
--存储过程中使用事务,并且加入异常处理机制. -- ============================================= CREATE PROCEDURE [dbo].[UP_ ...
- SQL中存储过程和自定义函数的区别(转载)
存储过程: 存储过程可以使得对数据库的管理.以及显示关于数据库及其用户信息的工作容易得多.存储过程是 SQL 语句和可选控制流语句的预编译集合,以一个名称存储并作为一个单元处理.存储过程存储在 ...
- SQL中Like语句的语法
在SQL结构化查询语言中,LIKE语句有着至关重要的作用. LIKE语句的语法格式是:select * from 表名 where 字段名 like 对应值(子串),它主要是针对字符型字段的,它的作用 ...
- 在Delphi中如何获得SQL中存储过程的返回值?
示例存储过程:create procedure proc_loginusername varchar(20),password varchar(20)asdeclare @result intsele ...
随机推荐
- java总结1
栈,堆,方法区.main和局部变量在栈,new 对象 在堆, 类和常量在方法区除了8大基础数据类型,其他都为引用变量局部变量在函数内或方法上声明,没有默认值,定义必须赋值一旦提供构造方法,就不会有默认 ...
- iview2.0 日期选择器DatePicker 所选时间格式不对
网上有很多解决方式,大部分都是加个@on-change事件.比如下图: 但是如果是编辑的时候,打开编辑页面,通过数据库返回时间显示出来是对的,但是不触发change事件,直接点保存的话,保存后还是少8 ...
- Shell中的特殊字符(三)
一 通配符 [root@192 test]# touch abc [root@192 test]# touch abcd [root@192 test]# touch 012 [root@192 te ...
- 虚树总结&题单&简要题解
简介 虚树,即剔除所有无关结点,只保留询问点和询问点的相关结点(两两之间的LCA),建一棵新树,这棵新树就是虚树.通过虚树,可以有效的减小询问(甚至修改)的复杂度.设询问点的个数是\(k\),那么建虚 ...
- Oracle--表有LONG类型复制或导数报ORA00990
SYS@racdb1> create table siebel.S_ORG_EXT_201707101650 as select * from siebel.S_ORG_EXT where 1= ...
- final修饰的类,其属性和方法默认是被final修饰的吗?
在论坛上,看到一个问题,当然,各位聪明的客官想必已经知道问题是什么了,嘿嘿,没错就是文章的标题:final修饰的类,其属性和方法默认是被final修饰的吗? 老实说,刚开始看到这个问题的时候,有点懵. ...
- Java中参数的引用传递和值传递
1.一些定义 值传递:把实际传入参数的值,在内存中赋值一份 引用传递:传入的是实际参数的地址 2.传递基本类型的参数 因为Java是值传递,那就不难理解下面的代码不交换任何值 swap(Type ar ...
- sock( ) bind( ) connect( )
Linux下的socket()函数 调用头文件<sys/socket.h>中的socket函数 int socket(int af, int type, int protocol); 1) ...
- web开发中会话跟踪的方法有哪些
会话跟踪就是浏览器和服务器通信 1.cookie 2.session 3.隐藏input 4.url重写 5.ip地址
- [VBA]批量新建指定名称的工作表
sub 批量新建指定名称的工作表() Dim i As Integer For i = 2 To 10 '根据实际情况修改i大小 Worksheets.Add after:=Worksheets ...