1、常用系统存储过程及使用语法:
exec sp_databases; --查看数据库
exec sp_tables; --查看表
exec sp_columns student;--查看列
exec sp_helpIndex student;--查看索引
exec sp_helpConstraint student;--约束
exec sp_stored_procedures;
exec sp_helptext '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;

2、用户自定义存储过程
2.1 创建不带参数存储过程:
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;

2.2 带输入参数存储过程
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;

2.3 有输入与输出参数的存储过程
create proc GetCommentCount
@newsid int,
@count int output
as
select @count=count(*) from Comment where NewsID=@newsid
--调用方法
declare @newsid int,
@count int;
set @newsid = 7;
exec GetCommentCount @newsid, @count output;
select @count;
print @count;

2.4 返回单个值的函数
create function MyFunction
(@newsid int)
returns int
as
begin
declare @count int
select @count=count(*) from Comment where NewsID=@newsid
return @count
end
--调用方法
declare @count int
exec @count=MyFunction 2
print @count

2.5 分页存储过程
--存储过程、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
--调用方法
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 asc) as number from student
) t
where t.number between @startRow and @endRow;
--调用方法
exec pro_stu 2, 2;

转载于:https://www.cnblogs.com/tlduck/p/5462419.html

SQL SERVER存储过程的几种示例相关推荐

  1. java调用存储过程 sql server_Java中调用SQL Server存储过程示例

    Java中调用SQL Server存储过程示例2007-09-03 08:48来源:论坛整理作者:孟子E章责任编辑:方舟·yesky评论(3) 最近做了个Java的小项目(第一次写Java的项目哦), ...

  2. SQL Server存储过程中使用表值作为输入参数示例

    这篇文章主要介绍了SQL Server存储过程中使用表值作为输入参数示例,使用表值参数,可以不必创建临时表或许多参数,即可向 Transact-SQL 语句或例程(如存储过程或函数)发送多行数据,这样 ...

  3. SQL Server存储过程初学者

    In this article, we will learn how to create stored procedures in SQL Server with different examples ...

  4. SQL Server 存储过程

    本章内容简介: • 存储过程的定义以及何时需要使用一个存储过程 • 如何创建.修改和删除存储过程 • 传递输入和输出参数的方式 • 错误处理 • 性能考虑事项 • 如何使用调试器 存储过程很有用.如果 ...

  5. SQL Server存储过程的基本概念以及语法【转】

    存储过程的概念        SQL Server提供了一种方法,它可以将一些固定的操作集中起来由SQL Server数据库服务器来完成,以实现某个任务,这种方法就是存储过程.        存储过程 ...

  6. SQL server 存储过程的建立和调用

    SQL server 存储过程的建立和调用 存储过程的建立和调用 --1.1准备测试需要的数据库:test,数据表:物料表,采购表 if not exists (select * from maste ...

  7. db2 删除存储过程_数据库教程-SQL Server存储过程使用及异常处理

    SQL Server存储过程 存储过程(Procedure)是数据库重要对象之一,也是数据库学习的重点之一.本文,我们以SQL Server为例对存储过程的概念.定义.调用.删除及存储过程调用异常等通 ...

  8. SQL Server存储过程基本语法

    存储过程的概念        SQL Server提供了一种方法,它可以将一些固定的操作集中起来由SQL Server数据库服务器来完成,以实现某个任务,这种方法就是存储过程.        存储过程 ...

  9. SQL Server存储过程输入参数使用表值

    在2008之前如果我们想要将表作为输入参数传递给SQL Server存储过程使比较困难的,可能需要很多的逻辑处理将这些表数据作为字符串或者XML传入. 在2008中提供了表值参数.使用表值参数,可以不 ...

  10. SQL Server存储过程里全库查找引用的数据库对象(表、存储过程等)

    SQL Server存储过程全库匹配数据库对象(表.存储过程等) 简介 可以通过自定义存储过程sp_eachdb来遍历每个数据库然后结合sys.objects 关联sys.sql_modules后的d ...

最新文章

  1. python删除过期文件_Python删除指定目录下的过期文件的代码
  2. 利用WCF的callback机制开发一个简单的多人游戏模型
  3. 好程序员web前端分享数组及排序、去重和随机点名
  4. DataGrid中的高级ToolTip
  5. 如何查看自己的电脑是否支持EFI引导?我的是dell 15r-488 N5010 谢谢了!
  6. Qt 中事件与处理
  7. 【牛客 - 318J】王者荣耀(dp,01背包)
  8. android Service Binder交互通信实例
  9. CentOS修改IP地址
  10. 正交幅度调制(QAM)信号的产生与解调介绍及matlab实现
  11. Windows下安装JanusGraph(踩坑记录)
  12. 太漂亮了!有了3款开源图标库,不用再去求设计师了
  13. 高等数学(预备知识之两角和差、二倍角与半角公式)
  14. 知识付费系统源码(开源知识付费系统平台下载)
  15. java 输出空心正方形_怎么用java数组打印一个正方形中间空着的
  16. FreeRtos延时函数delay_us()
  17. 矩阵求和c语言通俗易懂
  18. 对象、对象的属性、对象字面量、枚举对象中的属性、可变类型、变量和对象——JS对象
  19. 数据结构 :: 顺序栈与链式栈的设计与实现
  20. docker 部署nginx,挂载nginx.conf

热门文章

  1. PHP移动互联网开发笔记(6)——MySQL数据库基础回想
  2. 使用kubeadm安装kubenetes
  3. 机器学习(一):数据预处理
  4. 区分 点操作符+属性名 和 getAttribute()
  5. Firefox6 使用 firebug 解决方法 以及迅雷(thunder)插件报错
  6. Tasty项目经验总结(不断补充中)
  7. @JoinColumn 详解
  8. 李洪强iOS经典面试题30-一个区分度很大的面试题
  9. 定时备份windows机器上的文件到linux服务器上的操作梳理(rsync)
  10. Active Directory之强制占有操作主机