---
title: "我改写的sqlserver存储过程"
author: "ktz666"
date: "2009-11-07T13:10:00Z"
tags: ["Go"]
slug: "sqlserver"
draft: false
lastmod: "2009-11-07T13:10:00Z"
id: 559533
url: "https://ktz666.iteye.com/blog/559533"
---

![](http://images.cnblogs.com/cnblogs_com/lv_yantao/Images/OutliningIndicators/ContractedBlock.gif)![](http://images.cnblogs.com/cnblogs_com/lv_yantao/Images/OutliningIndicators/ExpandedBlockStart.gif)Code  
&lt;!--&lt;br /&gt;&lt;br /&gt;Code highlighting produced by Actipro CodeHighlighter (freeware)&lt;br /&gt;http://www.CodeHighlighter.com/&lt;br /&gt;&lt;br /&gt;--&gt;/\*declare @str as nvarchar(100)  
declare @str2 as nvarchar(1200)  
set @str2='2010-01-01'  
set  @str='select * from sq\_donglong.NwebCn\_NEws25 where NewsNameSi like ''%业绩%'' and AddTime&gt;'''+@str2+''''  
exec sp\_executesql @str  
print @@rowcount\*/  
CREATE PROCEDURE \[sq\_donglong].\[Achivement\_Back\_NewbCn\_News25]  
@iPageCount int=2 OUTPUT,--总页数  
@CountsIndex int =2 output,--总记录数  
@iPageSize int=2 ,--每页条数  
@iPageIndex int =2,--当前页  
@StartDate DateTime ,--开始日期  
@EndDate DateTime ,--结束日期  
@SearchKeyword varchar(500),--产品名称  
@SortID int--类别ID  
 AS

--创建临时表   
       create table #t(    \[ID] \[int] IDENTITY (1, 1),  
    \[NewsNameSi] \[nvarchar] (200) COLLATE Chinese\_PRC\_CI\_AS NULL ,  
    \[SortID] \[int] NULL ,  
    \[SortPath] \[nvarchar] (50) COLLATE Chinese\_PRC\_CI\_AS NULL ,  
    \[GroupID] \[nvarchar] (18) COLLATE Chinese\_PRC\_CI\_AS NULL ,  
    \[Exclusive] \[nvarchar] (2) COLLATE Chinese\_PRC\_CI\_AS NULL ,  
    \[SourceSi] \[nvarchar] (100) COLLATE Chinese\_PRC\_CI\_AS NULL ,  
    \[SmallPic] \[nvarchar] (200) COLLATE Chinese\_PRC\_CI\_AS NULL ,  
    \[BigPic] \[nvarchar] (200) COLLATE Chinese\_PRC\_CI\_AS NULL ,  
    \[ContentSi] \[ntext] COLLATE Chinese\_PRC\_CI\_AS NULL ,  
    \[ClickNumber] \[int] NULL ,  
    \[AddTime] \[smalldatetime] NULL ,  
    \[UploadFiles] \[ntext] COLLATE Chinese\_PRC\_CI\_AS NULL ,  
    \[NewFlag] \[bit] NULL ,  
    \[CommendFlag] \[bit] NULL ,  
    \[IsOk] \[bit] NULL ,  
    \[xuhao] \[int] NULL ,  
    \[Force] \[bit] NULL ,  
    \[AreaID] \[int] NULL )  
     ---变量allNums  
 declare @allNums as decimal  
 --查询条件  
 declare @datawhere as nvarchar(4000)  
 set @datawhere=''  
 select @allNums=count(ID) from sq\_donglong.NwebCn\_News25  
\--向临时表中写入数据  
declare @strsql nvarchar(4000)  
\--创建查询语句  
\--set  @strsql="select Pro\_id,Pro\_title,Pro\_passed,Pro\_addtime,Pro\_rank,Pro\_user,Pro\_class1,Pro\_small\_pic,Pro\_price from dbo.\[Pro\_Tbl\_Product]  where Pro\_id&gt;0 and Pro\_passed="+str(@passed)  
\--set @strsql="select ID,NewsNameSi,SortID,SortPath,ClickNumber,AddTime,IsOk,xuhao,Force,AreaID from sq\_donglong.\[NwebCn\_News25] where "

if(@SearchKeyword&lt;&gt;'')  
set @datawhere=@datawhere+' and NewsNameSi like ''%'+@SearchKeyword+'%''  or ContentSi like ''%'+@SearchKeyword+'%'''  
if(ISDATE(@StartDate)=1)  
set @datawhere=@datawhere+' and AddTime&gt;='''+@StartDate+''''  
if(ISDATE(@EndDate)=1)  
set @datawhere=@datawhere+' and AddTime&lt;='''+@EndDate+''''  
if(IsNumeric(@SortID)=1)  
set @strsql=@datawhere+' and SortID='+str(@SortID)+''  
set @strsql='select * from (select top '+@allNums+'ID,NewsNameSi,SortID,SortPath,ClickNumber,AddTime,IsOk,xuhao,Force,AreaID from sq\_donglong.NwebCn\_News25 where Force=1 '+@datawhere+' order by xuhao asc)a union all select * from (select top '+@allNums+' ID,NewsNameSi,SortID,SortPath,ClickNumber,AddTime,IsOk,xuhao,Force,AreaID from sq\_donglong.NwebCn\_News25 where Force&lt;&gt;1 '+@datawhere+' order by AddTime desc)b '  
declare @strsql2 nvarchar(4000)  
set @strsql2=@strsql  
\--插入  
  set @strsql='insert into #t(ID,NewsNameSi,SortID,SortPath,ClickNumber,AddTime,IsOk,xuhao,Force,AreaID) '+@strsql  
   execute sp\_executesql    @strsql  
    execute sp\_executesql    @strsql2  
\--确定总记录数  
    declare @Counts int  
    set @Counts = @@rowcount  
\--确定总页数  
   IF @Counts%@iPageSize=0  
          SET @iPageCount=CEILING(@Counts/@iPageSize)  
       ELSE  
          SET @iPageCount=CEILING(@Counts/@iPageSize)+1  
  --若请求的页号大于总页数，则显示最后一页  
      IF @iPageIndex &gt; @iPageCount  
        SELECT @iPageIndex = @iPageCount  
\--确定当前页的始末记录  
      DECLARE @iStart int    --start record  
      DECLARE @iEnd int      --end record  
     SELECT @iStart = (@iPageIndex - 1) * @iPageSize  
     SELECT @iEnd = @iStart + @iPageSize + 1  
\--取当前页记录      
    select * from #t where ID&gt;@iStart and ID&lt;@iEnd  
    set @CountsIndex =@@rowcount  
DROP TABLE #t  
return @Counts  
GO