SQLServer------存储过程的使用
转载:
http://www.cnblogs.com/hoojo/archive/2011/07/19/2110862.html
例子:
1.学生表
CREATE TABLE [dbo].[Student](
[StudentID] [int] IDENTITY(,) NOT NULL, --主键
[Number] [varchar]() NULL, --学号
[Name] [nchar]() NULL, --学生姓名
[ClassID] [int] NOT NULL --学生所在班级ID
) 插入学生数据:
declare @count int =;
while @count <
begin
insert into Student select @count,'学生'+ convert(varchar,@count,),cast(ceiling(rand() * ) as int)
set @count = @count + ;
end
2.教师表
create table Teacher(
[TeacherID] [int] IDENTITY(,) NOT NULL, --老师ID
[TeacherName] [nchar]() NULL, --老师名称
[ClassID] [int] NOT NULL -- 老师所教的班级ID
) 插入数据:
insert into Teacher select '陈老师',
insert into Teacher select '李老师',
insert into Teacher select '王老师',
insert into Teacher select '赵老师',
3.班级表
create table Class(
[ClassID] [int] IDENTITY(,) NOT NULL, --班级ID
[Code] [varchar]() NULL, --班级编号
[ClassName] [nchar]() NULL --班级名
) 插入班级数据:
insert into Class select '','计算机3班'
insert into Class select '','计算机1班'
insert into Class select '','计算机2班'
insert into Class select '','计算机5班'
insert into Class select '','计算机4班'
4.创建存储过程
create proc proc_getStudentRecord(
@pageIndex int, --页码
@pageSize int, --每页信息数
@name nchar() output --任课老师
)
as
declare @startRow int, @endRow int
set @startRow = (@pageIndex - ) * @pageSize +
set @endRow = @startRow + @pageSize - select s.Number,s.Name,b.Code,b.ClassName from(
select *, row_number() over (order by StudentID asc) as num from Student a where exists(select from Teacher t where a.ClassID = t.ClassID and t.TeacherName = @name)
) s
join
Class as b on
b.ClassID = s.ClassID
where s.num between @startRow and @endRow; go
4.执行存储过程
exec proc_getStudentRecord ,,'陈老师'
SQLServer------存储过程的使用的更多相关文章
- 解剖SQLSERVER 第十五篇 SQLSERVER存储过程的源文本存放在哪里?(译)
解剖SQLSERVER 第十五篇 SQLSERVER存储过程的源文本存放在哪里?(译) http://improve.dk/where-does-sql-server-store-the-sourc ...
- Sqlserver 存储过程中结合事务的代码
Sqlserver 存储过程中结合事务的代码 --方式一 if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[ ...
- SqlServer存储过程学习笔记(增删改查)
* IDENT_CURRENT 返回为任何会话和任何作用域中的特定表最后生成的标识值. CREATE PROCEDURE [dbo].[PR_NewsAffiche_AddNewsEntity] ( ...
- SQLServer 存储过程嵌套事务处理
原文:SQLServer 存储过程嵌套事务处理 某个存储过程可能被单独调用,也可能由其他存储过程嵌套调用,则可能会发生嵌套事务的情形. 下面是一种解决存储过程嵌套调用的通用代码,在不能确定存储过程是否 ...
- 创建并在项目中调用SQLSERVER存储过程的简单示例
使用SQLSERVER存储过程可以很大的提高程序运行速度,简化编程维护难度,现已得到广泛应用.创建存储过程 和数据表一样,在使用之前需要创建存储过程,它的简明语法是: 引用: Create PROC ...
- SQLSERVER存储过程语法详解
CREATE PROC [ EDURE ] procedure_name [ ; number ] [ { @parameter data_type } [ VARYING ] [ = default ...
- SqlServer存储过程详解
SqlServer存储过程详解 1.创建存储过程的基本语法模板: if (exists (select * from sys.objects where name = 'pro_name')) dro ...
- sqlServer存储过程与sql语句的区别
sqlServer 存储过程与sql语句的区别 sql存储过程与sql语句的区别: 从以下几个方面考虑: 1.编写: 存储过程:编写比较难: sql语句:相对简单: 2.性能: 存储过程:高,可移 ...
- SqlServer存储过程(增删改查)
* IDENT_CURRENT 返回为任何会话和任何作用域中的特定表最后生成的标识值. CREATE PROCEDURE [dbo].[PR_NewsAffiche_AddNewsEntity] ( ...
- SQLSERVER存储过程的基本语法实例
SQLSERVER存储过程的基本语法实例 SQLSERVER存储过程的基本语法实例 一.定义变量--简单赋值 declare @a intset @a=5 print @a --使用select语句赋 ...
随机推荐
- Markdown 11种基本语法【转】
[转自:http://www.cnblogs.com/hnrainll/p/3514637.html] 1. 标题设置(让字体变大,和word的标题意思一样)在Markdown当中设置标题,有两种方式 ...
- Curator入门教程1
简介 Curator是Netflix开源的一套ZooKeeper客户端框架. Netflix在使用ZooKeeper的过程中发现ZooKeeper自带的客户端太底层, 应用方在使用的时候需要自己处理 ...
- zTree V3 是个好东西 功能很强大
zTree V3 是个好东西 功能很强大 地址:http://www.ztree.me/v3/demo.php#_101
- Accumulator
Accumulator简介 Accumulator是spark提供的累加器,顾名思义,该变量只能够增加. 只有driver能获取到Accumulator的值(使用value方法),Task只能对其做增 ...
- Unity Vector3.Slerp使用范例
using System.Collections; using System.Collections.Generic; using UnityEngine; public class TestSler ...
- 【Unity】使用SceneManager加载/切换场景
一.直接切换 老版的(已弃用!): Application.LoadLevel(int index); // 参数是场景编号 Application.LoadLevel(string name); / ...
- JavaScrip——练习(做悬浮框进一步:悬浮窗后缀悬浮窗【感觉这种方法比较麻烦】)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Java-jdbc连接简化类jdbcUtil
在src文件夹下创建配置文件 db.properties db.properties drivername=com.mysql.jdbc.Driver url=jdbc:mysql://localho ...
- MongoDB 简介及安装
什么是MongoDB ? MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统. 在高负载的情况下,添加更多的节点,可以保证服务器性能. MongoDB 旨在为WEB应用提供 ...
- Fork of LGPL version of JPedal
https://github.com/on-site/JPedal —————————————————————————————————————————————————————————————————— ...