CREATE PROCEDURE p_QueryByPage_FinanceHistory
@tblName varchar(255), -- 表名
@strGetFields varchar(1000) = '*', -- 需要返回的列
@fldName varchar(255)='', -- 排序的字段名
@PageSize int = 10, -- 页尺寸
@StartPageIndex int = 1, -- 起始行数
@doCount bit = 0, -- 返回记录总数, 非 0 值则返回
@OrderType bit = 0, -- 设置排序类型, 非 0 值则降序
@strWhere varchar(1500) = '' -- 查询条件 (注意: 不要加 where)
AS
declare @strInsertSQL varchar(5000) --插入到新表语句
declare @strSQL varchar(5000) --sql语句
declare @strOrder varchar(400) -- 排序类型
declare @PageIndexEnd int --结束行数
set nocount on
if @doCount != 0
begin
if @strWhere !=''
set @strSQL = 'select count(*) as Total from ' + @tblName + ' where '+@strWhere
else
set @strSQL = 'select count(*) as Total from ' + @tblName
exec (@strSQL)
--print @strSQL
end
--以上代码的意思是如果@doCount传递过来的不是0,就执行总数统计。以下的所有代码都是@doCount为0的情况
else
begin
set @PageIndexEnd = @StartPageIndex + @PageSize
if @OrderType != 0
begin
set @strOrder = ' order by ' + @fldName +' desc'
--如果@OrderType不是0,就执行降序,这句很重要!
end
else
begin
set @strOrder = ' order by ' + @fldName +' asc'
end begin
--创建临时表
CREATE TABLE #TempTblFinanceHistory (
temp_id int IDENTITY(1,1) PRIMARY KEY,ID int,
Stock varchar(8),FQuarter int,ModifyDate int,ZGB float,LTGA float,LTGB float,
GDZS float,SYL float,MGYL float,MGJZC float,ZYLRL float,ZYZZL float,ZZCSYL float,
ZCFZBL float,ZYYWSR float,ZCZJ float,FZZJ float,GDQYHJ float,GJJ float,ZYYWLR float,
JLR float,TZSY float)
--插入数据
if @strWhere != ''
begin
set @strInsertSQL = 'INSERT INTO #TempTblFinanceHistory
SELECT ID,Stock,FQuarter,ModifyDate,ZGB,LTGA,LTGB,GDZS,SYL,MGYL,MGJZC,
ZYLRL,ZYZZL,ZZCSYL,ZCFZBL,ZYYWSR,ZCZJ,FZZJ,GDQYHJ,GJJ,ZYYWLR,JLR,TZSY
from ['+@tblName+']where '+ @strWhere
+ @strOrder
end
else
begin
set @strInsertSQL = 'INSERT INTO #TempTblFinanceHistory
SELECT ID,Stock,FQuarter,ModifyDate,ZGB,LTGA,LTGB,GDZS,SYL,MGYL,
MGJZC,ZYLRL,ZYZZL,ZZCSYL,ZCFZBL,ZYYWSR,ZCZJ,FZZJ,GDQYHJ,GJJ,
ZYYWLR,JLR,TZSY from ['+ @tblName +']'+ @strOrder
end
exec (@strInsertSQL)
--查询
set nocount oFF
select temp_id,ID,Stock,FQuarter,ModifyDate,ZGB,LTGA,LTGB,GDZS,SYL,MGYL,
MGJZC,ZYLRL,ZYZZL,ZZCSYL,ZCFZBL,ZYYWSR,ZCZJ,FZZJ,GDQYHJ,GJJ,ZYYWLR,JLR,TZSY
from [#TempTblFinanceHistory]
where ([temp_id]>=@StartPageIndex)and ([temp_id] <@PageIndexEnd)
set nocount on
DROP TABLE #TempTblFinanceHistory
end
end
GO
试试这个怎么样?
@tblName varchar(255), -- 表名
@strGetFields varchar(1000) = '*', -- 需要返回的列
@fldName varchar(255)='', -- 排序的字段名
@PageSize int = 10, -- 页尺寸
@StartPageIndex int = 1, -- 起始行数
@doCount bit = 0, -- 返回记录总数, 非 0 值则返回
@OrderType bit = 0, -- 设置排序类型, 非 0 值则降序
@strWhere varchar(1500) = '' -- 查询条件 (注意: 不要加 where)
AS
declare @strInsertSQL varchar(5000) --插入到新表语句
declare @strSQL varchar(5000) --sql语句
declare @strOrder varchar(400) -- 排序类型
declare @PageIndexEnd int --结束行数
set nocount on
if @doCount != 0
begin
if @strWhere !=''
set @strSQL = 'select count(*) as Total from ' + @tblName + ' where '+@strWhere
else
set @strSQL = 'select count(*) as Total from ' + @tblName
exec (@strSQL)
--print @strSQL
end
--以上代码的意思是如果@doCount传递过来的不是0,就执行总数统计。以下的所有代码都是@doCount为0的情况
else
begin
set @PageIndexEnd = @StartPageIndex + @PageSize
if @OrderType != 0
begin
set @strOrder = ' order by ' + @fldName +' desc'
--如果@OrderType不是0,就执行降序,这句很重要!
end
else
begin
set @strOrder = ' order by ' + @fldName +' asc'
end begin
--创建临时表
CREATE TABLE #TempTblFinanceHistory (
temp_id int IDENTITY(1,1) PRIMARY KEY,ID int,
Stock varchar(8),FQuarter int,ModifyDate int,ZGB float,LTGA float,LTGB float,
GDZS float,SYL float,MGYL float,MGJZC float,ZYLRL float,ZYZZL float,ZZCSYL float,
ZCFZBL float,ZYYWSR float,ZCZJ float,FZZJ float,GDQYHJ float,GJJ float,ZYYWLR float,
JLR float,TZSY float)
--插入数据
if @strWhere != ''
begin
set @strInsertSQL = 'INSERT INTO #TempTblFinanceHistory
SELECT ID,Stock,FQuarter,ModifyDate,ZGB,LTGA,LTGB,GDZS,SYL,MGYL,MGJZC,
ZYLRL,ZYZZL,ZZCSYL,ZCFZBL,ZYYWSR,ZCZJ,FZZJ,GDQYHJ,GJJ,ZYYWLR,JLR,TZSY
from ['+@tblName+']where '+ @strWhere
+ @strOrder
end
else
begin
set @strInsertSQL = 'INSERT INTO #TempTblFinanceHistory
SELECT ID,Stock,FQuarter,ModifyDate,ZGB,LTGA,LTGB,GDZS,SYL,MGYL,
MGJZC,ZYLRL,ZYZZL,ZZCSYL,ZCFZBL,ZYYWSR,ZCZJ,FZZJ,GDQYHJ,GJJ,
ZYYWLR,JLR,TZSY from ['+ @tblName +']'+ @strOrder
end
exec (@strInsertSQL)
--查询
set nocount oFF
select temp_id,ID,Stock,FQuarter,ModifyDate,ZGB,LTGA,LTGB,GDZS,SYL,MGYL,
MGJZC,ZYLRL,ZYZZL,ZZCSYL,ZCFZBL,ZYYWSR,ZCZJ,FZZJ,GDQYHJ,GJJ,ZYYWLR,JLR,TZSY
from [#TempTblFinanceHistory]
where ([temp_id]>=@StartPageIndex)and ([temp_id] <@PageIndexEnd)
set nocount on
DROP TABLE #TempTblFinanceHistory
end
end
GO
试试这个怎么样?
解决方案 »
- 怎样把“2009-08-01 00:00:00.000”改成“2009-08-01”?
- SQL 语句求救
- 求存储过程!!!!在线等…………
- 问一问题,帮一下忙~
- 求助!关于sql 2005 维护计划的问题
- odbc中建立系统DSN出错
- 大哥们帮帮忙,一个查询的问题
- winxp(pro)下可以安装sql2000的哪些版本,标准班可以么?在线等!
- sql客户端怎样也连不通sql sever服务器端怎麽会事,急!!!!
- 求助。analysis services和reporting services在哪
- SQL Server2000 安装失败
- 我发了两个关于sql的问题,现在还是没有解决.没有人能给个正确的方案么?
---- 这里返回记录集了,应该是赋值给一个变量才对吧
if @doCount != 0
begin
if @strWhere !=''
set @strSQL = 'select count(*) as Total from ' + @tblName + ' where '+@strWhere
else
set @strSQL = 'select count(*) as Total from ' + @tblName
exec (@strSQL)
--print @strSQL
end
--以上代码的意思是如果@doCount传递过来的不是0,就执行总数统计。以下的所有代码都是@doCount为0的情况-- 改成如下就不是返回记录集了set @strsql = 'select @totalrecords = count(*) from ' + @tblName + ' where '+@strWhere
exec(@strsql)