存储过程怎么写判断?if else ?

yison1128 2010-04-25 09:08:19
我在写一个查询的存储过程
在存储过程中写个判断
type是传入的参数
如果type=0
查询的条件就是

select * from t_ProInfo where ProName like '%@sContent%'

如果type!=0
查询条件就是

select * from t_ProInfo where ProName like '%@sContent%' and type=@type

怎么做?
...全文
925 30 打赏 收藏 转发到动态 举报
写回复
用AI写文章
30 条回复
切换为时间正序
请发表友善的回复…
发表回复
wanghuaide 2010-04-26
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 wuyq11 的回复:]

declare @sql varchar(200)
if(@typw=0)
set @sql =@sql+' AND ProName LIKE '''+@sContent+''''
else set @sql =@sql+''
[/Quote]
type写进@sql里了,但,条件写反了吧
if(@typw=0) 应该是这样if(@typw!=0)
wanghuaide 2010-04-26
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 yison1128 的回复:]

引用 1 楼 wuyq11 的回复:
declare @sql varchar(200)
if(@typw=0)
set @sql =@sql+' AND ProName LIKE '''+@sContent+''''
else set @sql =@sql+''

type去哪了
[/Quote]
他忘记写了
wanghuaide 2010-04-26
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 sxiaohui8709253 的回复:]

SQL code

if @type=0
begin
select * from t_ProInfo where ProName like '%@sContent%'

end
else
begin
select * from t_ProInfo where ProName like '%@sContent%' and type=@type

end
[/Quote]
这样就行
jbz001 2010-04-26
  • 打赏
  • 举报
回复
我只是路过
zhangguofang1129 2010-04-26
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 lost_painting 的回复:]

如果是一句

IF 條件
分支1
ELSE
分支2

多句
IF 條件
BEGIN
分支一
END
ELSE
BEGIN
分支二
END
[/Quote]

就可以了
scdn8311 2010-04-26
  • 打赏
  • 举报
回复
像这种基本的东西,查看一下帮助就OK了,没必要发出来吧!
HatoLee 2010-04-26
  • 打赏
  • 举报
回复
这年头抢分不容易啊……
dengyongyao1985 2010-04-26
  • 打赏
  • 举报
回复
Create proc t_ProInfo

@type int,
@sContent varchar(50)

as
if(@type=0)
begin
select * from t_ProInfo where ProName like '%@sContent%'
end
else
begin
select * from t_ProInfo where ProName like '%@sContent%' and type=@type
end
go
porschev 2010-04-26
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 lost_painting 的回复:]
如果是一句

IF 條件
分支1
ELSE
分支2

多句
IF 條件
BEGIN
分支一
END
ELSE
BEGIN
分支二
END
[/Quote]

.
ganxiaotu 2010-04-26
  • 打赏
  • 举报
回复

if @type=0
begin
select * from t_ProInfo where ProName like '%@sContent%'
end
else
begin
select * from t_ProInfo where ProName like '%@sContent%' and type=@type
end
fonvey 2010-04-26
  • 打赏
  • 举报
回复
markup
fwqkey 2010-04-26
  • 打赏
  • 举报
回复
都回答了....
fwind2008 2010-04-26
  • 打赏
  • 举报
回复
Create proc 存储过程名称
@sContent varchar(50),
@type int
as
if(@type=0)
begin
select * from t_ProInfo where ProName like '%@sContent%'
end
else
begin
select * from t_ProInfo where ProName like '%@sContent%' and type=@type
end
go
coco_ke 2010-04-25
  • 打赏
  • 举报
回复
晕,改了前面忘记后面

select * from t_ProInfo where ProName like '%'+@sContent+'%' and (type=@type or @type=0)


coco_ke 2010-04-25
  • 打赏
  • 举报
回复

select * from t_ProInfo where ProName like '%'+@sContent+'%' and type=@type

coco_ke 2010-04-25
  • 打赏
  • 举报
回复

select * from t_ProInfo where ProName like '%@sContent%' and (type=@type or @type=0)


ikiss 2010-04-25
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 yison1128 的回复:]

不行,like ‘%@sContent%’
好像不正确,查不到东西
要怎么写?
[/Quote]

没内容SQL没有接收到@sContent参数,可以这样写:

declare @sql nvarchar(300)
set @sql='select * from t_ProInfo where ProName like ''%'+@sContent+'%'' and type=case when @type=0 then type else @type end'
execute sp_executesql @sql,N'@type int',@type
ikiss 2010-04-25
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 yison1128 的回复:]

不行,like ‘%@sContent%’
好像不正确,查不到东西
要怎么写?
[/Quote]

不用写得像上边那么麻烦,一行SQL就可以搞定:
select * from t_ProInfo where ProName like '%@sContent%' and type=case when @type=0 then type else @type end
vip__888 2010-04-25
  • 打赏
  • 举报
回复
if @type=0
begin
exec @sql
end
else
begin
exec @sql
end
wcl2222 2010-04-25
  • 打赏
  • 举报
回复
2L高人!
加载更多回复(10)

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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