sql存储过程,根据多个条件查询,返回一个dataTable或者DataSet

Posted 楚景然

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql存储过程,根据多个条件查询,返回一个dataTable或者DataSet相关的知识,希望对你有一定的参考价值。

不废话,先直接代码

首先sql部分(我在这里加了一个@zx作为判断,一般不需要这个参数):

ALTER Proc [dbo].[Proc_Boss_Show]
@StrIndex varchar(500),--开始条数
@EndIndex varchar(500),--结束条数
@StrWhere  varchar(max),
@zx int --执行区间  1执行 top n  2显示所有数据
as

begin
     declare  @strSqlstr varchar(500)
     
     if(@zx=1)
     begin
         set @strSqlstr=select  * from (select top +@EndIndex
         set @strSqlstr+=  row_number() over(order by 提货时间 desc)
         as xx,* from View_Boss_Show  where 1=1 +@StrWhere
         set @strSqlstr+= ) as xx where xx>+@StrIndex


    end
     if(@zx=2)
    begin
       set @strSqlstr=select * from  View_Boss_Show  where  1=1 +@StrWhere
    end

print @strSqlstr
exec(@strSqlstr)

asp.net代码(直接用DataTable接收):

DataTable dt = null;
            try
            {
                dt = DBHelper.GetDataTable("Proc_View_show1391", para);
            }
            catch (Exception ex)
            { //throw new Exception(ex.ToString());
            }

 

以上是关于sql存储过程,根据多个条件查询,返回一个dataTable或者DataSet的主要内容,如果未能解决你的问题,请参考以下文章

SQL Server 存储过程中处理多个查询条件的几种常见写法分析,我们该用那种写法

如何从 Access 2010 中的 SQL 存储过程返回多个记录集

SQL多个条件模糊查询问题。。。

oracle中的存储过程如何返回查询到的多个值?

使用 RODBC 将 SQL 存储过程结果转换为 data.frame 格式

SQL多条件模糊查询解决方案-存储过程