分页

--使用master数据库

use master

go
--判断数据库中是否含有Data数据库

if exists(select name from sys.databases where name='HaHa')drop database HaHa

go
--创建Data数据库

create database HaHa

go
--使用Data数据库

use HaHa

go
if object_id('TableOne') is not null

drop table TableOne

go
set nocount on

go

create table TableOne(_id varchar(50),_name varchar(50),_sex char(2)constraint Class_id_FK primary key(_id))

go

set nocount off

go
set nocount on

go

insert into TableOne(_id,_name,_sex) values('1476','哈哈','男')

go

set nocount off

go
select * from TableOne

go
if object_id('TableTwo') is not null

drop table TableTwo

go
create table TableTwo([id] varchar(50),[name] varchar(50),[sex] char(2))

go
insert into TableTwo select _id,_name,_sex from TableOne

go
----或者--select _id,_name,_sex into TableTwo from TableOne--go
--要是采用上面的insert into那么第二张表一定要存在不然不能将第一张表中的数据插入第二张表的--要是采用下面的select那么第二张表一定不能存在的,不然会出现错误说表已经存在的
select * from TableTwo

go
if object_id('Exec_') is not nulldrop procedure Exec_

go
create procedure Exec_ 

@startIndex int,

@endIndex int,

@count bit

as

if(@count=1)select count(0) from TableOne

else

begin

declare @TableTwo table(_newid int identity(1,1),TableOne_id int)

set rowcount @endIndex

insert into @TableTwo(TableOne_id) select _id from TableOne order by _id desc

select * from TableOne,@TableTwo Two where TableOne._id=Two.TableOne_id and Two.TableOne_id between @startIndex and @endIndex order by Two._newidend

原文地址:https://www.cnblogs.com/meroselove/p/1917452.html