存储过程编写 多个条件的

小土bibox 2011-11-01 03:39:41
编写存储过程时遇到了点问题,


忘各位大虾不吝赐教,小弟在此谢过。。。。。。



问题:

create Procedure Proc_ShowBug
(
@UserID int,
@ProjectID int,
@CategoryID int,
@statuID int
)
as
select .......from tblTable
where UserID = @UserID
and ProjectID = @ProjectID
and CategoryID = @CategoryID
and statuID = @statuID
go



这个半成品中的参数后三个有可能是0,如果是0就不用这个条件,如果非零才用and,在存储过程里怎么判断呢,

这个select语句怎么组织呢。

必须用存储过程,c#里的逻辑已经写好了。所以。。。。。。。


急急急。。。在线等。在此谢过
...全文
97 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
小土bibox 2011-11-01
  • 打赏
  • 举报
回复
功能通过测试,呵呵。谢谢各位
yhui1989love 2011-11-01
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 fredrickhu 的回复:]
引用 7 楼 yhui1989love 的回复:
引用 5 楼 dawugui 的回复:
where UserID = @UserID
and ProjectID = (case when @ProjectID = 0 then ProjectID else @ProjectID end)
and CategoryID = (case when @CategoryID = 0 then C……
[/Quote]
搞个CASE when 感觉把问题复杂了 or就解决了啊
我是这么认为滴
--小F-- 2011-11-01
  • 打赏
  • 举报
回复
基本可以作为一个三目运算符

当满足前面条件的时候 得到一个值 不满足 得到else 后面的值
koumingjie 2011-11-01
  • 打赏
  • 举报
回复


create Procedure Proc_ShowBug
(
@UserID int,
@ProjectID int,
@CategoryID int,
@statuID int
)
as
DECLARE @sql NVARCHAR(MAX)

SET @sql='select * from tb where UserID=' + @UserID
IF (@ProjectID!=0)
set @sql=@sql + ' AND ProjectID=' + @ProjectID
IF (@CategoryID!=0)
set @sql=@sql + ' AND CategoryID=' + @CategoryID
IF (@statuID!=0)
set @sql=@sql + ' AND statuID=' + @statuID

EXEC sp_executesql @sql
go


geniuswjt 2011-11-01
  • 打赏
  • 举报
回复
可读性比你那写法强太多了[Quote=引用 7 楼 yhui1989love 的回复:]

引用 5 楼 dawugui 的回复:
where UserID = @UserID
and ProjectID = (case when @ProjectID = 0 then ProjectID else @ProjectID end)
and CategoryID = (case when @CategoryID = 0 then CategoryID else @CategoryI……
[/Quote]
--小F-- 2011-11-01
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 yhui1989love 的回复:]
引用 5 楼 dawugui 的回复:
where UserID = @UserID
and ProjectID = (case when @ProjectID = 0 then ProjectID else @ProjectID end)
and CategoryID = (case when @CategoryID = 0 then CategoryID else @CategoryID……
[/Quote]

case when 就是一个判断而已。
yhui1989love 2011-11-01
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 dawugui 的回复:]
where UserID = @UserID
and ProjectID = (case when @ProjectID = 0 then ProjectID else @ProjectID end)
and CategoryID = (case when @CategoryID = 0 then CategoryID else @CategoryID end)
and statuID = ……
[/Quote]
用CASE WHEN 有什么秒处?
yhui1989love 2011-11-01
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 qianjin036a 的回复:]
SQL code

create Procedure Proc_ShowBug
(
@UserID int,
@ProjectID int,
@CategoryID int,
@statuID int
)
as
select .......from tblTable
where UserID = @UserID
and ProjectID =(case when @Pro……
[/Quote]
不用这么就我上面那样写的 昨天我还用来着
理解下 不难啊!
dawugui 2011-11-01
  • 打赏
  • 举报
回复
where UserID = @UserID
and ProjectID = (case when @ProjectID = 0 then ProjectID else @ProjectID end)
and CategoryID = (case when @CategoryID = 0 then CategoryID else @CategoryID end)
and statuID = (case when @statuID = 0 then CategoryID else @CategoryID end)
-晴天 2011-11-01
  • 打赏
  • 举报
回复
create Procedure Proc_ShowBug
(
@UserID int,
@ProjectID int,
@CategoryID int,
@statuID int
)
as
select .......from tblTable
where UserID = @UserID
and ProjectID =(case when @ProjectID<>0 then @ProjectID else ProjectID end)
and CategoryID = (case when @CategoryID<>0 then @CategoryID else CategoryID end)
and statuID = (case when @statuID<>0 then @statuID else statuID end)
go

-晴天 2011-11-01
  • 打赏
  • 举报
回复
create Procedure Proc_ShowBug
(
@UserID int,
@ProjectID int,
@CategoryID int,
@statuID int
)
as
select .......from tblTable
where UserID = @UserID
and ProjectID =(case when @ProjectID>0 then @ProjectID else ProjectID end)
and CategoryID = (case when @CategoryID>0 then @CategoryID else CategoryID end)
and statuID = (case when @statuID>0 then @statuID else statuID end)
go

yhui1989love 2011-11-01
  • 打赏
  • 举报
回复
C#参数传进来要为0 这个(statuID = @statuID or @statuID = 0)就等于(statuID = 0 or 0=0)就是恒等 所以这个参数也就不用了
yhui1989love 2011-11-01
  • 打赏
  • 举报
回复
这个好办:
create Procedure Proc_ShowBug
(
@UserID int,
@ProjectID int,
@CategoryID int,
@statuID int
)
as
select .......from tblTable
where UserID = @UserID
and (ProjectID = @ProjectID or @ProjectID = 0 )
and (CategoryID = @CategoryID or @CategoryID)
and (statuID = @statuID or @statuID = 0)
go

拿分来 我没有分了 ...


22,300

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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