在SQL中存储过程的一般语法
一般分为十种情况,每种语法各不相同:
1、 创建语法
| 1 2 3 4 5 6 7 | createproc | procedurepro_name   [{@参数数据类型} [=默认值] [output],    {@参数数据类型} [=默认值] [output],    ....   ]as   SQL_statements | 
2、 创建不带参数存储过程
| 1 2 3 4 5 6 7 8 9 10 | --创建存储过程if (exists (select* fromsys.objects wherename= 'proc_get_student'))    dropproc proc_get_studentgocreateproc proc_get_studentas    select* fromstudent;--调用、执行存储过程execproc_get_student; | 
3、 修改存储过程
| 1 2 3 4 | --修改存储过程alterproc proc_get_studentasselect* fromstudent; | 
4、 带参存储过程
| 1 2 3 4 5 6 7 8 9 10 | --带参存储过程if (object_id('proc_find_stu', 'P') isnotnull)    dropproc proc_find_stugocreateproc proc_find_stu(@startId int, @endId int)as    select* fromstudent whereid between@startId and@endIdgoexecproc_find_stu 2, 4; | 
5、 带通配符参数存储过程
| 1 2 3 4 5 6 7 8 9 10 11 | --带通配符参数存储过程if (object_id('proc_findStudentByName', 'P') isnotnull)    dropproc proc_findStudentByNamegocreateproc proc_findStudentByName(@namevarchar(20) = '%j%', @nextName varchar(20) = '%')as    select* fromstudent wherenamelike@nameandnamelike@nextName;goexecproc_findStudentByName;execproc_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') isnotnull)    dropproc proc_getStudentRecordgocreateproc proc_getStudentRecord(    @id int, --默认输入参数    @namevarchar(20) out, --输出参数    @age varchar(20) output--输入输出参数)as    select@name= name, @age = age  fromstudent whereid = @id andsex = @age;go-- declare@id int,        @namevarchar(20),        @tempvarchar(20);set@id = 7; set@temp= 1;execproc_getStudentRecord @id, @nameout, @tempoutput;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') isnotnull)    dropproc proc_tempgocreateproc proc_tempwithrecompileas    select* fromstudent;goexecproc_temp; | 
8、 加密存储过程
| 1 2 3 4 5 6 7 8 9 10 11 12 13 | --加密WITH ENCRYPTION if (object_id('proc_temp_encryption', 'P') isnotnull)    dropproc proc_temp_encryptiongocreateproc proc_temp_encryptionwithencryptionas    select* fromstudent;goexecproc_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') isnotnull)    dropproc proc_cursorgocreateproc proc_cursor    @cur cursorvaryingoutputas    set@cur = cursorforward_only staticfor    selectid, name, age fromstudent;    open@cur;go--调用declare@exec_cur cursor;declare@id int,        @namevarchar(20),        @age int;execproc_cursor @cur = @exec_cur output;--调用存储过程fetchnextfrom@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    fetchnextfrom@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') isnotnull)    dropproc proc_cursorgocreateproc pro_page    @startIndex int,    @endIndex intas    selectcount(*) fromproduct;        select* from(        selectrow_number() over(orderbypid) asrowId, * fromproduct     ) temp    wheretemp.rowId between@startIndex and@endIndexgo--drop proc pro_pageexecpro_page 1, 4----分页存储过程if (object_id('pro_page', 'P') isnotnull)    dropproc pro_stugocreateprocedurepro_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 (orderbyid asc) asnumber fromstudent     ) t    wheret.number between@startRow and@endRow;execpro_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 ... 
随机推荐
- 查看 XML 文件
			在所有现代浏览器中,均能够查看原始的 XML 文件. 不要指望 XML 文件会直接显示为 HTML 页面. 查看 XML 文件 查看这个 XML 文件:note.xml 打开 XML 文件 - XML ... 
- Markers
			immune pdf(file = paste0(outdir,"T_B_NK_feature.pdf")) VlnPlot(expr_1_4,features = c(" ... 
- sh_05_偶数求和
			sh_05_偶数求和 # 计算 0 ~ 100 之间 所有 偶数 的累计求和结果 # 开发步骤 # # 1. 编写循环 确认 要计算的数字 # 2. 添加 结果 变量,在循环内部 处理计算结果 # 1 ... 
- Pycharm,出现Invalid VCS root mapping The directory 解决方法
			Pycharm File 中setting-------version control 中VCS选择none 后选择ok 执行完以上的步骤,还错误就会消失. 
- 测试常用shell命令
			正则表达式 特殊字符 $ . ' * [ ] ^ | ( ) \ + ? awk使用心得 将抽取域在屏幕上显示和保存到文件中 awk '{print $3}' t1.txt | tee file1.t ... 
- 【每日一包0007】array-range
			[github地址:https://github.com/ABCDdouyae...] array-range 生成一个指定起始位置的固定长度的数组 用法:array-range(start, end ... 
- socket通信(TCP和UDP)
			1.TCP 2.UDP 
- Arthas随笔
			目录 Arthas 安装Java 安装 Arthas Arthas 命令及示例 源码分析 Arthas 安装Java 下载jdk 注意 下载的JDK版本要与linux操作系统相匹配,否则汇报No su ... 
- 【机器学习速成宝典】模型篇05朴素贝叶斯【Naive Bayes】(Python版)
			目录 先验概率与后验概率 条件概率公式.全概率公式.贝叶斯公式 什么是朴素贝叶斯(Naive Bayes) 拉普拉斯平滑(Laplace Smoothing) 应用:遇到连续变量怎么办?(多项式分布, ... 
- reduce、map、zip、filter使用记录
			注意:结果取完一次就没了: # -*- coding:utf-8 -*- ### functools.reduce from functools import reduce r1 = reduce(l ... 
