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

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


不知道各位理解没?
...全文
122 11 打赏 收藏 转发到动态 举报
写回复
用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
【无人机】基于改进粒子群算法的无人机路径规划研究[和遗传算法、粒子群算法进行比较](Matlab代码实现)内容概要:本文围绕基于改进粒子群算法的无人机路径规划展开研究,重点探讨了在复杂环境中利用改进粒子群算法(PSO)实现无人机三维路径规划的方法,并将其与遗传算法(GA)、标准粒子群算法等传统优化算法进行对比分析。研究内容涵盖路径规划的多目标优化、避障策略、航路点约束以及算法收敛性和寻优能力的评估,所有实验均通过Matlab代码实现,提供了完整的仿真验证流程。文章还提到了多种智能优化算法在无人机路径规划中的应用比较,突出了改进PSO在收敛速度和全局寻优方面的优势。; 适合人群:具备一定Matlab编程基础和优化算法知识的研究生、科研人员及从事无人机路径规划、智能优化算法研究的相关技术人员。; 使用场景及目标:①用于无人机在复杂地形或动态环境下的三维路径规划仿真研究;②比较不同智能优化算法(如PSO、GA、蚁群算法、RRT等)在路径规划中的性能差异;③为多目标优化问题提供算法选型和改进思路。; 阅读建议:建议读者结合文中提供的Matlab代码进行实践操作,重点关注算法的参数设置、适应度函数设计及路径约束处理方式,同时可参考文中提到的多种算法对比思路,拓展到其他智能优化算法的研究与改进中。

34,872

社区成员

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

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