多表联合查询sql存储过程带分页

note:分页可能不是特别好,把搜索的结果插入到临时表里面,然后再进行分页,感觉速度会慢,但是不知道怎么解决

sql代码:

USE [opussys_db]
GO
/****** Object:  StoredProcedure [dbo].[proc_DataSearch]    Script Date: 2015-03-04 14:52:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Mofijeck>
-- Create date: <2015-03-04>
-- Description:	<三张表联合搜索>
-- =============================================
ALTER PROCEDURE [dbo].[proc_DataSearch]
	-- Add the parameters for the stored procedure here
	(
	@words nvarchar(200) ,
	@type int ,
	@startIndex int ,
	@endIndex int
	)
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	create table #Tmp  --创建临时表#Tmp
    (
      ID   int IDENTITY (1,1)     not null, --创建列ID,并且每次新增一条记录就会加1
	  searchid int,      
	  updateTime datetime,	  
	  stype int,
      primary key (ID)      --定义ID为临时表#Tmp的主键      
    )
	truncate table #Tmp


	if @type=0
	begin
	--新闻
	insert into #Tmp 
	select id,creattime,1 from table1 where col like '%'+rtrim(@words)+'%'
	--end
	--作家
	insert into #Tmp
	select id,creattime,2 from table2 where col like '%'+rtrim(@words)+'%'
	--end
	--作品
	insert into #Tmp
	select id,creattime,3 from table3 where col like '%'+rtrim(@words)+'%'
    --end
	end

	if @type=1
	begin
	--新闻
	insert into #Tmp 
	select id,creattime,@type from table1 where col like '%'+rtrim(@words)+'%'
	end

	if @type=2
	begin
	--作家
	insert into #Tmp
	select id,creatTime,@type from table2 where col like '%'+rtrim(@words)+'%'
	end

	if @type=3
	begin
	--作品
	insert into #Tmp
	select id,creattime,@type from table3 where col like '%'+rtrim(@words)+'%'
	end
	

	
	--返回结果
	SELECT * FROM (
	   SELECT ROW_NUMBER() OVER (
	      order by T.updateTime desc
	   ) AS Row, T.*  from #Tmp T
	) as TT where TT.Row between  @startIndex and @endIndex

	--计算记录数
    declare @count int
    select @count=count(ID) from #Tmp
    return @count

    -- Insert statements for procedure here
	
END

C#获取返回结果值和结果集:

#region
        /// <summary>
        /// 
        /// </summary>
        /// <param name="words">关键词 </param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static DataSet searchByWords(string words, int type,int startIndex,int endIndex) { 
            IDataParameter[] parameter ={
                                            new SqlParameter("@words",SqlDbType.NVarChar,200),
                                            new SqlParameter("@type",SqlDbType.Int,4),
                                            new SqlParameter("@startIndex",SqlDbType.Int,4),
                                            new SqlParameter("@endIndex",SqlDbType.Int,4)
                                       };
            parameter[0].Value = words;
            parameter[1].Value = type;
            parameter[2].Value = startIndex;
            parameter[3].Value = endIndex;
            DataSet ds = DbHelperSQL.RunProcedure("proc_DataSearch", parameter, "search");
            return ds;
        }
        /// <summary>
        /// 返回结果
        /// </summary>
        /// <param name="words"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static int searchByWordsCount(string words, int type, int startIndex, int endIndex)
        {
            IDataParameter[] parameter ={
                                            new SqlParameter("@words",SqlDbType.NVarChar,200),
                                            new SqlParameter("@type",SqlDbType.Int,4),
                                            new SqlParameter("@startIndex",SqlDbType.Int,4),
                                            new SqlParameter("@endIndex",SqlDbType.Int,4)
                                       };
            parameter[0].Value = words;
            parameter[1].Value = type;
            parameter[2].Value = startIndex;
            parameter[3].Value = endIndex;
            int results = 0;
            int returnCount = DbHelperSQL.RunProcedure("proc_DataSearch", parameter, out results);
            return returnCount;
        }
        #endregion


  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值