22,301
社区成员




USE [egov]
GO
/****** Object: StoredProcedure [dbo].[PROC_SYSTEM_Pagination] Script Date: 05/13/2014 13:23:01 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
ALTER proc [dbo].[PROC_SYSTEM_Pagination]
(
@PageIndex int, --第N页
@Sql nvarchar(4000), --SQL语句
@Order varchar(50), --排序字段
@Direction varchar(10),--排序方向
@PageSize int, --每页的大小
@Count int output --总计录数
--@PageCount int output --总页数
)
AS
BEGIN
--计算边界
SELECT @PageIndex=(CASE WHEN @PageIndex<1 THEN 0 ELSE @PageIndex-1 END)
--计算总记录数
Declare @totalRecord int
Declare @countsql nvarchar(4000)
SELECT @countsql='select @totalRecord=count(*) from ( '+@Sql+' ) tb '
EXEC sp_executesql @countsql,N'@totalRecord int OUTPUT',@totalRecord=@Count OUTPUT
Declare @SQL1 nvarchar(4000)
declare @i int
set @i=CHARINDEX('select',@Sql)
set @Sql=substring(@Sql,@i,6)+' top '+cast(@Count as varchar)+' '+substring(@Sql,@i+6,len(@Sql)-6)
if @Order=''
begin
SET @Order='id'
SET @Direction='asc'
SET @SQL1='select rowid=identity(int,1,1),* into #t from (' +@Sql +' order by '+@Order+' '+@Direction+') tb '+';'+
'select * from #t where rowid>='+cast(@PageSize*@PageIndex+1 as varchar)+' and rowid<='+cast(@PageSize*@PageIndex+@PageSize as varchar)
end
else
begin
SET @SQL1='select rowid=identity(int,1,1),* into #t from (' +@Sql +' order by '+@Order+' '+@Direction+',id '+@Direction+') tb '+';'+
'select * from #t where rowid>='+cast(@PageSize*@PageIndex+1 as varchar)+' and rowid<='+cast(@PageSize*@PageIndex+@PageSize as varchar)
end
EXEC (@SQL1)
END
create table testtt (
id int identity(1,1) not null,
name varchar(10)
)
insert into testtt values('abc')
select rowid=identity(int,1,1),* into #a from testtt
select * into #a from testtt
。。。具体怎么改,稍等一会,我看看你的脚本[/quote]
恩,具体问题确实是这个,如果只查一个表我知道怎么改,只要给主键*1就可以了,但是现在问题因为不是只查一个表,只能用*来查找全部,所有没法确定主键,这个就让我挠头了[/quote]
不止一个表---这个确实有点让人挠头。。。
那就用刚给你的方法二,把rowid=identity(int,1,1)去掉试试吧。。。[/quote]
我也知道 把rowid去掉就万事大吉了,但是老板不让去掉
--你可以在where条件后面加上条件T.name=@table ,进而列出所有表。
SELECT
T.[name] AS [表名] ,
AC.[name] AS [列名] ,
TY.[name] AS [系统数据类型],
AC.[is_identity]
FROM sys.[tables] AS T
INNER JOIN sys.[all_columns] AC ON T.[object_id] = AC.[object_id]
INNER JOIN sys.[types] TY ON AC.[system_type_id] = TY.[system_type_id]
AND AC.[user_type_id] = TY.[user_type_id]
WHERE T.[is_ms_shipped] = 0
ORDER BY T.[name] ,
AC.[column_id]
这个只是一个思路,具体要写的话,估计还比较复杂,考虑的东西应该还很多。。。
说服你老板把那个rowid=identity去掉呗。。。create table testtt (
id int identity(1,1) not null,
name varchar(10)
)
insert into testtt values('abc')
select rowid=identity(int,1,1),* into #a from testtt
select * into #a from testtt
。。。具体怎么改,稍等一会,我看看你的脚本[/quote]
恩,具体问题确实是这个,如果只查一个表我知道怎么改,只要给主键*1就可以了,但是现在问题因为不是只查一个表,只能用*来查找全部,所有没法确定主键,这个就让我挠头了[/quote]
不止一个表---这个确实有点让人挠头。。。
那就用刚给你的方法二,把rowid=identity(int,1,1)去掉试试吧。。。[/quote]
我也知道 把rowid去掉就万事大吉了,但是老板不让去掉create table testtt (
id int identity(1,1) not null,
name varchar(10)
)
insert into testtt values('abc')
select rowid=identity(int,1,1),* into #a from testtt
select * into #a from testtt
。。。具体怎么改,稍等一会,我看看你的脚本[/quote]
恩,具体问题确实是这个,如果只查一个表我知道怎么改,只要给主键*1就可以了,但是现在问题因为不是只查一个表,只能用*来查找全部,所有没法确定主键,这个就让我挠头了[/quote]
不止一个表---这个确实有点让人挠头。。。
那就用刚给你的方法二,把rowid=identity(int,1,1)去掉试试吧。。。create table testtt (
id int identity(1,1) not null,
name varchar(10)
)
insert into testtt values('abc')
select rowid=identity(int,1,1),* into #a from testtt
select * into #a from testtt
。。。具体怎么改,稍等一会,我看看你的脚本[/quote]
恩,具体问题确实是这个,如果只查一个表我知道怎么改,只要给主键*1就可以了,但是现在问题因为不是只查一个表,只能用*来查找全部,所有没法确定主键,这个就让我挠头了create table testtt (
id int identity(1,1) not null,
name varchar(10)
)
insert into testtt values('abc')
select rowid=identity(int,1,1),* into #a from testtt
select * into #a from testtt
。。。具体怎么改,稍等一会,我看看你的脚本