狗取什么名字好听:关于SQL SERVER存储过程的问题[高手进,在线等]

来源:百度文库 编辑:神马品牌网 时间:2024/04/30 17:49:19
设定多个输入参数
如何根据输入参数进行一个表内的复合查询?
如某个输入参数不为空则进行字段匹配查询,如为空则不加入匹配。
我知道啊
一般都是
sql=select from....where
IF 参数1<>"" then
sql=sql+....
end if
if 参数2<>"" then
sql=sql+....
end if
sql=sql+....
rs.open sql,conn,3,3
这样的对吧?但是这个东东在存储过程里面怎么写啊?

给你个例子吧:

CREATE PROCEDURE cx
(
@page int=1, ----当前第几页
@pagesize int=10, -----每页的记录数目
@pagecount int output, ------总共页数
@counts int output, ----总共记录数目
@cxwhere varchar(100)=null , ----查询条件
@cxtable varchar(500)=null, ----查询表格
@cxorder varchar(100)=null , -----查询条件
@cxid varchar(50)=null -------主键
)
as

declare @tmpsql nvarchar(1000)
declare @tmpstr nvarchar(500)
Declare @intCounts int ----要移动的记录数
Declare @BeginID int ----开始的ID
Declare @EndID int ----结束的ID
Declare @strID nvarchar(1000) ----存放取得查询开头或结尾ID的查询语句

set @tmpstr='select @counts=count ('+@cxid+' ) from '+@cxtable+' where '+@cxwhere
exec sp_executesql @tmpstr,N'@counts int out',@counts out
---计算分页总数
if @Counts <= @pageSize
set @pageCount = 1
else
set @pageCount = (@Counts / @pageSize) + 1
--------防止数据溢出
if @page<1
set @page=1
if @page>@pagecount
set @page=@pagecount

--计算要移动的记录数
set @intCounts = (@page-1) * @pageSize + 1

-----取得分页后此页的第一条记录的ID
set @strID = 'select @BeginID='+@cxid+' from '+@cxtable+' where '+@cxwhere+' order by '+@cxorder

set rowcount @intCounts
exec sp_executesql @strID,N'@BeginID int out ',@BeginID out

-----取得分页后此页的最后一条记录的ID
set @intCounts = @intCounts + @pageSize - 1
set rowcount @intCounts
exec sp_executesql @strID,N'@BeginID int out ',@EndID out

set rowcount 0

if @cxwhere is null
set @tmpsql = 'select * from '+@cxtable+' where '+@cxwhere+' and '+@cxid+' between ' + str(@BeginID) + ' and ' + str(@EndID)+' order by '+@cxorder
else
set @tmpsql = 'select * from '+@cxtable+' where '+@cxwhere+' and '+@cxid+' between ' + str(@EndID) + ' and ' + str(@BeginID) +' order by '+@cxorder

exec sp_executesql @tmpsql
GO

你在这里等14天啊

我不怎么懂哦

复合查询? 用高级 查询语句哦

我以ASP.NET(C#)中为例

首先要设置字段参数
string 参数1,参数2;
string 字段参数1,字段参数2;
字段参数1="";
字段参数2="";
参数1="";
参数2="";
//调用一个函数对各个参数进行传值,不传就查所有
string sqlstr;
sqlstr="select * from 表 where "+字段参数1+"="+参数1+","+字段参数2"="参数2;

//执行