以下给出存储过程代码和鄙人运行成功的截图。把以下存储过程代码中的tablename替换为自己的表名。

一 不带参数存储过程

if (exists (select * from sys.objects where name = 'proc_get_tablename'))
    drop proc proc_get_tablename
go
create proc proc_get_tablename
as
    select * from tablename;

二 带参存储过程

if (object_id('proc_find_tablename', 'P') is not null)
    drop proc proc_find_tablename
go
create proc proc_find_tablename(@startId int, @endId int)
as
    select * from tablename where id between @startId and @endId
go

三 带通配符参数存储过程

if (object_id('proc_findtablenameByName', 'P') is not null)
    drop proc proc_findtablenameByName
go
create proc proc_findtablenameByName(@name varchar(20) = '%j%', @nextName varchar(20) = '%')
as
    select * from tablename where name like @name and name like @nextName;
go

四 带输出参数存储过程
if (object_id('proc_gettablenameRecord', 'P') is not null)
    drop proc proc_gettablenameRecord
go
create proc proc_gettablenameRecord(
    @id int, --默认输入参数
    @name varchar(20) out, --输出参数
    @age varchar(20) output--输入输出参数
)
as
    select @name = name, @age = age  from tablename where id = @id and sex = @age;
go

五 不缓存存储过程
--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 tablename;
go

六 加密存储过程****
--加密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 tablename;
go

可以看到加密存储过程显示的图标与别的不同,有一个小锁的标识;

七 带游标参数存储过程
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 tablename;
    open @cur;
go

如果出现 必须声明标量变量 "@cur" 错误;
可试着加入SET NOCOUNT ON

编写如下的执行语句,然后执行,
USE dboa;
GO
DECLARE @MyCursor CURSOR;
exec dbo.proc_cursor @cur=@MyCursor OUTPUT;
WHILE (@@FETCH_STATUS = 0)
BEGIN;
     FETCH NEXT FROM @MyCursor;
END;
CLOSE @MyCursor;
DEALLOCATE @MyCursor;
GO

结果如下图,一行一行的提取了行;

关于 须声明标量变量 "@cur" 错误,网友有言:
变量的使用必须和定义在一起
1.declare @name varchar(30) = 'aaaa'
print @name
2.declare @name varchar(30) = 'aaaa'
go
print @name
1可以执行,而2由于声明与使用分开,就会报错

八分页存储过程1

---存储过程、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 tablename
;    
    select * from (
        select row_number() over(order by pid) as rowId, * from tablename 
    ) temp
    where temp.rowId between @startIndex and @endIndex
go

执行前提示输入参数,此处每页5条;当然实际需要分页的情况都是记录数很多;例如鄙人曾做过10万记录每页分1万的情况;

九 分页存储过程2
if (object_id('pro_page', 'P') is not null)
    drop proc pro_tablename
go
create procedure pro_tablename(
    @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 tablename 
    ) t
    where t.number between @startRow and @endRow;

输入参数是起始行号,页尺寸;

示例数据库 dboa 下载:

http://pan.baidu.com/s/1bntWzSV

全部存储过程代码下载:

http://pan.baidu.com/s/1o6kgAP8

图解SQL Server 存储过程教程一相关推荐

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

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

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

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

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

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

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

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

  5. Microsoft SQL Server 存储过程

    Microsoft SQL Server 存储过程 TRIGGER DDL触发器:主要用于防止对数据库架构.视图.表.存储过程等进行的某些修改:DDL事件是指对数据库CREATE,ALTER,DROP ...

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

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

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

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

  8. SQL Server存储过程初学者

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

  9. oracle如何调试sql,调试oracle与调试sql server存储过程

    [IT168 技术]关于存储过程的调试,知道方法以后很简单,但在不知道的时候,为了测试一个存储过程的正性,print,插入临时表等可谓是使出了浑身解数,烦不胜烦.下面就把我工作中调试oracle存储过 ...

最新文章

  1. html精灵图坐标如何确定,如何使用HTML中的精灵图
  2. WebStorm无法显示文件夹目录
  3. 如何升级xcode 中的cocos2dx 到v2.2.2以上版本
  4. colinux php,利用colinux 搭建linux开发环境
  5. python format函数实例_python中强大的format函数实例详解
  6. Bootstrap三角箭头.caret 类
  7. 硬件故障-笔记本电脑开不了机故障处理
  8. 剑指offer58 二叉树的下一个结点
  9. 【第一篇】Volley的使用之json请求
  10. linux tomcat6安装及配置
  11. 《微观经济学》第三章相互依存性与贸易的好处
  12. 饥荒联机版服务器启动慢_饥荒联机版大型攻略——简介与目录
  13. 查看mysql数据库密码_如何查看mysql数据库的登录名和密码
  14. mysql临时表关联查询_MySQL如何执行关联查询
  15. 4874: 筐子放球
  16. 3d巧用计算机算胆,3D巧用函数公式精准定三胆
  17. 战战兢兢尝试tensorflow2.0
  18. laravel安装laravel-s
  19. 十一假期,我在头等舱里,看到了自已贫穷的真相!
  20. 串口转以太网通信源代码C语言C++编写支持多路转换双向通信支持UDP和TCP客户端 提供

热门文章

  1. Work From Anywhere
  2. Get Started with Apex的playground练习
  3. 分支结构||分支循环结构||使用原生js遍历对象
  4. js GB2312和unicode互转
  5. Docker selenium自动化 - 使用python操作docker,python运行、启用、停用和查询容器实例演示
  6. 不支持图形化界面的Linux系统如何显示图像化界面?飞腾服务器显示图像化界面方法,DISPLAY environment variable is undefined问题解决方法
  7. Python 图像处理篇-利用opencv库展示本地图片实例演示
  8. freeRtos学习笔记 (8) 任务通知
  9. CTFshow 命令执行 web76
  10. poj 3150 Cellular Automaton(迷糊,但原理是用的快速幂)