一般分为十种情况,每种语法各不相同:

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_student
go
create proc proc_get_student
as
    select from student;
 
--调用、执行存储过程
exec proc_get_student;

3、 修改存储过程

1
2
3
4
--修改存储过程
alter proc proc_get_student
as
select 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_stu
go
create proc proc_find_stu(@startId int, @endId int)
as
    select from student where id between @startId and @endId
go
 
exec 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_findStudentByName
go
create proc proc_findStudentByName(@name varchar(20) = '%j%', @nextName varchar(20) = '%')
as
    select from student where name like @name and name like @nextName;
go
 
exec 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_getStudentRecord
go
create 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_temp
go
create proc proc_temp
with recompile
as
    select from student;
go
 
exec 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_encryption
go
create proc proc_temp_encryption
with encryption
as
    select from student;
go
 
exec 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_cursor
go
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 (<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);
end
close @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_cursor
go
create proc pro_page
    @startIndex int,
    @endIndex int
as
    select count(*) from product
;    
    select from (
        select row_number() over(order by pid) as rowId, * from product 
    temp
    where temp.rowId between @startIndex and @endIndex
go
--drop proc pro_page
exec pro_page 1, 4
--
--分页存储过程
if (object_id('pro_page''P'is not null)
    drop proc pro_stu
go
create 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 ascas number from student 
    ) t
    where t.number between @startRow and @endRow;
 
exec pro_stu 2, 2;

在SQL中存储过程的一般语法的更多相关文章

  1. SQL中存储过程和函数的区别

    转:https://www.cnblogs.com/jacketlin/p/7874009.html 本质上没区别.只是函数有如:只能返回一个变量的限制.而存储过程可以返回多个. 而函数是可以嵌入在s ...

  2. 面试问题 - SQL 中存储过程与函数的区别

    SQL 中的存储过程与函数没有本质上的区别 函数 -> 只能返回一个变量. 函数可以嵌入到sql中使用, 可以在select 中调用, 而存储过程不行.  但函数也有着更多的限制,比如不能使用临 ...

  3. Sql 中存储过程详细案例

    转自:http://www.cnblogs.com/yank/p/4235609.html 概念 存储过程(Stored Procedure):已预编译为一个可执行过程的一个或多个SQL语句. 创建存 ...

  4. SQL中存储过程和自定义函数的区别

    存储过程:     存储过程可以使得对数据库的管理.以及显示关于数据库及其用户信息的工作容易得多.存储过程是 SQL 语句和可选控制流语句的预编译集合,以一个名称存储并作为一个单元处理.存储过程存储在 ...

  5. SQL中存储过程的例子

    导读:sql存储是数据库操作过程中比较重要的一个环节,对于一些初学者来说也是比较抽象难理解的,本文我将通过几个实例来解析数据库中的sql存储过程,这样就将抽象的事物形象化,比较容易理解. 例1: cr ...

  6. SQL中存储过程中使用事务,并且加入异常处理机制.

    --存储过程中使用事务,并且加入异常处理机制. -- ============================================= CREATE PROCEDURE [dbo].[UP_ ...

  7. SQL中存储过程和自定义函数的区别(转载)

    存储过程:     存储过程可以使得对数据库的管理.以及显示关于数据库及其用户信息的工作容易得多.存储过程是 SQL 语句和可选控制流语句的预编译集合,以一个名称存储并作为一个单元处理.存储过程存储在 ...

  8. SQL中Like语句的语法

    在SQL结构化查询语言中,LIKE语句有着至关重要的作用. LIKE语句的语法格式是:select * from 表名 where 字段名 like 对应值(子串),它主要是针对字符型字段的,它的作用 ...

  9. 在Delphi中如何获得SQL中存储过程的返回值?

    示例存储过程:create procedure proc_loginusername varchar(20),password varchar(20)asdeclare @result intsele ...

随机推荐

  1. PHP+FLASH大文件断点续传功能分享

    1.使用PHP的创始人 Rasmus Lerdorf 写的APC扩展模块来实现(http://pecl.php.net/package/apc) APC实现方法: 安装APC,参照官方文档安装,可以使 ...

  2. Spring Data Jpa (二)JPA基础查询

    介绍Spring Data Common里面的公用基本方法 (1)Spring Data Common的Repository Repository位于Spring Data Common的lib里面, ...

  3. oracle 11g安装教程终结版

    1.解压文件 2.双击 “setup.exe” 3.高级安装.下一步 4.企业版  下一步 5.下一步 6.只要不报错,警告没事.(路径如果报错的话,没事,直接继续) 注意:如果有需要6.2版本的错误 ...

  4. SQL Server 2016升级迁移过程中性能问题诊断案例

    日常运行的批量更新作业,平日是5分钟之内结束,今天出现超过30分钟没结束的情况,实际运行3个小时以上,应用程序超时报错. 数据库版本:SQL Server 2016企业版 问题SQL: declare ...

  5. Cordova-在现有iOS工程自动化接入Cordova插件

    模拟Cordova插件命令 自己编写脚本,了解cordova添加插件做了哪些事情. 上一篇文章了解到,web与native的交互主要是cordova.js中的exec方法调用,触发交互事件.UIWeb ...

  6. web编辑器的快捷键

    alt 键:可以同时选中多个光标 .box 按住 tab 键 可以快速的 <div class="box"></div> #box 按住 tab 键 可以快 ...

  7. MFC ATL STL概要

    MFC-----应用程序框架     ATL-----写COM的利器     STL-----用来写逻辑部分 MFC:   MFC的目标是桌面应用,当然也有网络部分但很不充分.MFC是一套APP   ...

  8. web开发中会话跟踪的方法

    1. 什么是会话 会话是指一个终端用户(服务器)与交互系统(客户端)进行通讯的过程. 2. 什么是会话跟踪 对同一个用户对服务器的连续的请求和接受响应的监视.(将用户与同一用户发出的不同请求之间关联, ...

  9. robot framework 接口自动化之登录

    网络不便,好久没更了,颓废好久,惭愧 目录 1.安装必须的库 2.固定格式介绍 3.完成一个登录 1.安装必须的库 requestsLibrary.requests安装 1.pip install r ...

  10. windows7如何用键盘模拟鼠标操作

    windows7如何用键盘模拟鼠标操作 https://jingyan.baidu.com/article/6dad5075104907a123e36e38.html 听语音 37453人看了这个视频 ...