问个存储过程的问题

FrameSniper 2008-10-09 09:11:12
我现在有个存储过程,想对表udfdt_Department进行查询

表中字段有三个字段,分别是df_Index(smallint)、df_Name(nvarchar(16))和df_SuperiorDeptIndex(smallint)

我现在要求的存储过程是根据界面输入的对应df_Name和df_SuperiorDeptIndex两个字段的内容去查询

可是客户在输入的时候可能不输入,不输入就查询全部记录。输入就按照输入条件去查询

下面是我写的存储过程

create procedure udsp_SelectDepartmentRecordContent
@Name nvarchar(16),
@SuperiorDeptName nvarchar(16)
as
declare @SuperiorDeptIndex smallint
set @SuperiorDeptIndex=(select df_Index from udfdt_Department where df_Name=@SuperiorDeptName)
declare @SelectStatement nvarchar(512)
set @SelectStatement='select * from udfdt_Department where df_Name='''+IsNull(@Name,'*')+''' and @SuperiorDeptIndex='+IsNull(convert(nvarchar(10),@SuperiorDeptIndex),*)
execute(@SelectStatement)

go

但感觉好像不对,请问各位这个存储过程应该怎么写?

...全文
125 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
netcup 2008-10-09
  • 打赏
  • 举报
回复
动态筛选:

create procedure udsp_SelectDepartmentRecordContent
@Name nvarchar(16),
@SuperiorDeptName nvarchar(16)
as
declare @sql nvarchar(4000)
set @sql=N'select * from udfdt_Department where 1=1
'+N'case when @name is not null then N'and name=@oid else N'' END
'+N' CASE WHEN @SuperiorDeptName IS NOT NULL THEN N' AND SuperiorDeptName=@SuperiorDeptName' ELSE N'' end

exec(@sql)

yygyogfny 2008-10-09
  • 打赏
  • 举报
回复
我这也是,搜索条件比较多,客户有可能输了,也有可能没有输入条件..

我就是这么做的,用if语句写动态连接太麻烦了..
yygyogfny 2008-10-09
  • 打赏
  • 举报
回复
try:
where 1=1
and (aa like '%' + @aa + '%' or isnull(aa,'') = '')
and (bb like N'%' + @bb+ '%' or isnull(bb,'') = '')
and (cc like N'%' + @cc+ '%' or isnull(cc,'') = '')
and (cc like N'%' + @cc+ '%' or isnull(@cc,'') = '')
sunylf 2008-10-09
  • 打赏
  • 举报
回复
create procedure udsp_SelectDepartment
@Name nvarchar(16),
@SuperiorDeptName nvarchar(16)
as
declare @SuperiorDeptIndex smallint
set @SuperiorDeptIndex=isnull((select df_Index from udfdt_Department where df_Name=@SuperiorDeptName),0)
declare @SelectStatement nvarchar(512)
set @SelectStatement='select * from udfdt_Department where df_Name like '+'''+'%'+isnull(@Name,'*')+'%'+'''+' and @SuperiorDeptIndex like '+'''+'%'+convert(nvarchar(10),@SuperiorDeptIndex) +'%'+'''
execute(@SelectStatement)

go
fcuandy 2008-10-09
  • 打赏
  • 举报
回复
形似
create proc p1
@a int,
@b varchar(10),
@c int
as
begin
select * from tb where fa=isnull(@a,fa) and fb like '%' + isnull(@b,fb) + '%' and fc = isnull(@c,fc)
end
FrameSniper 2008-10-09
  • 打赏
  • 举报
回复
没人会?

现在还有个问题,如果查询条件少了还可以用if语句去做判断,但要是查询条件非常多,用if判断是非常麻烦的!

谁有什么好的方法可以解决这个问题?
FrameSniper 2008-10-09
  • 打赏
  • 举报
回复
like怎么表示条件为空呢?
sunylf 2008-10-09
  • 打赏
  • 举报
回复
這個有必要用存儲過程麼?
用like替換=.再加%呢。
ailliy 2008-10-09
  • 打赏
  • 举报
回复
给大家介绍一个J2EE、.NET视频下载站(免费哦)
免费视频:
浪曦ASP.NET企业实战系列 http://down1.langsin.com/001.rar
浪曦NUnit详解视频 http://down1.langsin.com/002.rar
浪曦Struts 2应用开发详解 http://www.verycd.com/topics/210454
VIP视频:
浪曦Java常见笔试、面试题目深度剖析
浪曦Lucene视频教程
培训视频
业务QQ:1050429531
FrameSniper 2008-10-09
  • 打赏
  • 举报
回复
create procedure udsp_SelectDepartment
@Name nvarchar(16),
@SuperiorDeptName nvarchar(16)
as
declare @SuperiorDeptIndex smallint
set @SuperiorDeptIndex=isnull((select df_Index from udfdt_Department where df_Name=@SuperiorDeptName),0)----当界面上没有设置条件df_SuperDeptName的时候,这里我想通过下面的语句查处所有记录,怎么赋默认值
declare @SelectStatement nvarchar(512)
set @SelectStatement='select * from udfdt_Department where df_Name='''+isnull(@Name,'*')+''' and @SuperiorDeptIndex='+convert(nvarchar(10),@SuperiorDeptIndex)----同样,如果没有设置界面的df_Name内容的时候,怎么给默认值可以查询所有记录
execute(@SelectStatement)

go
FrameSniper 2008-10-09
  • 打赏
  • 举报
回复
create procedure udsp_SelectDepartment
@Name nvarchar(16),
@SuperiorDeptName nvarchar(16)
as
declare @SuperiorDeptIndex smallint
set @SuperiorDeptIndex=isnull((select df_Index from udfdt_Department where df_Name=@SuperiorDeptName),0)
declare @SelectStatement nvarchar(512)
set @SelectStatement='select * from udfdt_Department where df_Name='''+isnull(@Name,'*')+''' and @SuperiorDeptIndex='+convert(nvarchar(10),@SuperiorDeptIndex)
execute(@SelectStatement)

go
FrameSniper 2008-10-09
  • 打赏
  • 举报
回复
我的分不是很多了,以后有分再散给大家!

34,594

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧