求一个存储过程,望高手回答~~~

scocsdn 2004-12-23 06:22:18
我现在有四个参数@a,@b,@c,@d,都是字符串
select * from tb
where a=@a and b=@b and c=@c and d=@d
现在有一个要求:当其中任意一个参数@c=""时想让@c不参加查询
也就是where a=@a and b=@b and d=@d


不知道各位理解没?
...全文
114 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
scocsdn 2004-12-23
  • 打赏
  • 举报
回复
谢谢,用的是第一种方法

先成功了一半

然后发现char和varchar有很大的区别

改了一下,成功!!!!!!!!
playyuer 2004-12-23
  • 打赏
  • 举报
回复
agree lmj2003(雁不归)

select *
from tb
where
(a = @a or @a = '' or @a is null)
and
(b = @b or @b = '' or @b is null)
chinaandys 2004-12-23
  • 打赏
  • 举报
回复
第一种方法:一开始设为null,是为了避免前台(如delphi)没有输入参数值,所设的默认值.

第一种方法是这样的: @flag是个标记,当判断参数不为空时,@flag就加1,然后再判断第二个参数,如果还不为空,并且@flag<>0那么就加二个条件..。。。。。

//如果@a="" @b="2" @c="3"
那@sql=select * from tb where a="" and b="2" and c="3"

数据表a字段没有""数据,楼主你仔细看一看 @sql这一句(where a="" and b="2" and c="3")
你就设置条件a="",可是你a字段没有""数据,当然一条数据也没有了..

lmj2003 2004-12-23
  • 打赏
  • 举报
回复
where (a="" )or a=@a
...
playyuer 2004-12-23
  • 打赏
  • 举报
回复
select *
from tb
where
case when isnull(@a,'') = ''
then ''
else a
end like isnull(@a,'')
and
case when isnull(@b,'') = ''
then ''
else b
end like isnull(@b,'')
and
case when isnull(@c,'') = ''
then ''
else c
end like isnull(@c,'')
and
case when isnull(@d,'') = ''
then ''
else d
end like isnull(@d,'')
scocsdn 2004-12-23
  • 打赏
  • 举报
回复
我顶了又顶
scocsdn 2004-12-23
  • 打赏
  • 举报
回复
先谢谢楼上的,可能还是没理解我,郁闷那~~~~

如果@a="" @b="2" @c="3"
那@sql=select * from tb where a="" and b="2" and c="3"

但数据表a字段没有""数据,就会出现一条数据也找不到

我看了你的第一种方法,不知道参数怎么传过来,你一开始就设为null了

解决加分,再次谢谢
chinaandys 2004-12-23
  • 打赏
  • 举报
回复
这样的,是不:
create procedure test( @a varchar(12),@b varchar(12),@c varchar(12),@d varchar(12))
as
begin
declare @sql varchar(3000)
set @sql=''
if @a<>"" or @b<>"" or @c<>""
begin
set @sql='select * from tb where a='''+@a+''' and b='''+@b+''' and d='''+@d+''''
exec(@sql)
end
return
end

scocsdn 2004-12-23
  • 打赏
  • 举报
回复
不是楼上那高级玩意

我现在查一个表的时候,当某个参数为""时就没有数据显示出来了
我想当参数为""时把他从查询条件里去掉
chinaandys 2004-12-23
  • 打赏
  • 举报
回复
期待更好的,二楼
chinaandys 2004-12-23
  • 打赏
  • 举报
回复
楼主就是想要一个查询器,是不:

这是俺编的,你参照一下,可能有些哆嗦:

-----------------------------------------------------

create procedure sp_goodsinfo
(@id0 varchar(12)=null,
@name0 varchar(15)=null,
@corptype varchar(15)=null,
@sort varchar(12)=null
)
as
declare @flag int,@sql nvarchar(150)
set @flag=0
set @sql='select * from goods_info'
begin
if (@id0 is not null)
begin
set @sql=@sql+' where goodsid like ''%'+@id0+'%'''
set @flag=@flag+1
end
if (@name0 is not null)
begin
if (@flag=0)
set @sql=@sql+' where goodsname like ''%'+@name0+'%'''
else
set @sql=@sql+' and goodsname like ''%'+@name0+'%'''
set @flag=@flag+1
end
if (@corptype is not null)
begin
if (@flag=0)
set @sql=@sql+' where proid like ''%'+@corptype+'%'''
else
set @sql=@sql+' and proid like ''%'+@corptype+'%'''
set @flag=@flag+1
end
if (@sort is not null)
begin
if (@flag=0)
set @sql=@sql+' where sort='''+@sort+''''
else
set @sql=@sql+' and sort='''+@sort+''''
set @flag=@flag+1
end

exec(@sql)
return
end

34,837

社区成员

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

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