SQL SERVER存储过程的几种示例
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;
SQL SERVER存储过程的几种示例的更多相关文章
- SQL Server 存储过程的几种常见写法分析,我们该用那种写法
本文出处: http://www.cnblogs.com/wy123/p/5958047.html 最近发现还有不少做开发的小伙伴,在写存储过程的时候,在参考已有的不同的写法时,往往很迷茫,不知道各种 ...
- SQL Server存储过程中使用表值作为输入参数示例
这篇文章主要介绍了SQL Server存储过程中使用表值作为输入参数示例,使用表值参数,可以不必创建临时表或许多参数,即可向 Transact-SQL 语句或例程(如存储过程或函数)发送多行数据,这样 ...
- SQL Server 存储过程(转载)
SQL Server 存储过程 Transact-SQL中的存储过程,非常类似于Java语言中的方法,它可以重复调用.当存储过程执行一次后,可以将语句缓存中,这样下次执行的时候直接使用缓存中的语句.这 ...
- (摘录)SQL Server 存储过程
文章摘录:http://www.cnblogs.com/hoojo/archive/2011/07/19/2110862.html SQL Server 存储过程 Transact-SQL中的存储过程 ...
- SQL Server存储过程Return、output参数及使用技巧
SQL Server目前正日益成为WindowNT操作系统上面最为重要的一种数据库管理系统,随着 SQL Server2000的推出,微软的这种数据库服务系统真正地实现了在WindowsNT/2000 ...
- SQL Server 存储过程具体解释
SQL Server 存储过程具体解释 存储过程的优缺点 ◆长处: 运行速度更快. 存储过程仅仅在创造时进行编译,而一般SQL语句每运行一次就编译一次,所以使用存储过程运行速度更快. 存储过程用于处理 ...
- .net(C#数据库访问) Mysql,Sql server,Sqlite,Access四种数据库的连接方式
便签记录Mysql,Sql server,Sqlite,Access四种数据库的简单连接方式 //using MySql.Data.MySqlClient; #region 执行简单SQL语句,使用M ...
- SQL server分页的四种方法
SQL server分页的四种方法 1.三重循环: 2.利用max(主键); 3.利用row_number关键字: 4.offset/fetch next关键字 方法一:三重循环思路 先取前20页, ...
- sql server 存储过程 output 和return的使用 方法,详解
SQL Server目前正日益成为WindowNT操作系统上面最为重要的一种数据库管理系统,随着 SQL Server2000的推出,微软的这种数据库服务系统真正地实现了在WindowsNT/2000 ...
随机推荐
- Docker 搭建一个Docker应用栈
Docker应用栈结构图 Build Django容器 编写docker-file FROM django RUN pip install redis build django-with-redis ...
- C++中substr的用法
C++中substr函数的用法 #include<string> #include<iostream> using namespace std; void main() { s ...
- Java中的UDP协议编程
一. UDP协议定义 UDP协议的全称是用户数据报,在网络中它与TCP协议一样用于处理数据包.在OSI模型中,在第四层——传输层,处于IP协议的上一层.UDP有不提供数据报分组.组装和不能对数据包 ...
- sql 数据库中只靠一个数据,查询到所在表和列名
有时候我们想通过一个值知道这个值来自数据库的哪个表以及哪个字段,在网上搜了一下,找到一个比较好的方法,通过一个存储过程实现的.只需要传入一个想要查找的值,即可查询出这个值所在的表和字段名. 前提是要将 ...
- SpringMVC中响应json数据(异步传送)
1.首先导入3个jar包: jackson-annotations-2.1.5.jar jackson-core-2.1.5.jar jackson-databind-2.1.5.jar JSON所需 ...
- 循环插入一条数据的sql写法
DECLARE @i INTSET @i = 1WHILE @i > 0 BEGIN DECLARE @TransportFormMstID BIGINT; DECLARE @TradeOrde ...
- review20
Pattern与Matcher类 模式匹配就是检索和指定模式匹配的字符串.java提供了专门用来进行模式匹配的Pattern类和Matcher类,这些类在java.util.regex包中. 模式对象 ...
- MySQL 基础理论面试题整理
前言: 之前整理公司面试题的时候,看了一篇大神些 SQL 优化之六脉神剑 文章,写的真好! 博主有一些 MySQL 的面试题,简单抽了一个备注一下,补充一下自己不熟悉的地方. 一.在MySQL中, ...
- JProfiler连接weblogic
转 http://blog.csdn.net/xu1314/article/details/7737236
- 延时并自动关闭MessageBox
信息提示框(MessageBox)是微软NET自带的一个用于弹出警告.错误或者讯息一类的“模式”对话框.此类对话框一旦开启,则后台窗体无法再被激活(除非当前的MessageBox被点击或者关闭取消). ...