求一SQL语句。在线.....

bondhu 2003-09-29 11:35:37
我想实现如下功能:
declare @flag char(1) --在运行时副职

select a.id,a.name
from test a
where a.type = ( case @flag when 'A' then 'a' or 'c' when 'B' then 'b'or 'd' end )

上述语法是错的,主要是最后的case语句有误。我的目的是,如果@flag = 'A'则检索type = 'a'or 'c'的记录。如果@flag = 'B'则检索type = 'b'or 'd'的记录。
如何实现呢?
...全文
48 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
aierong 2003-09-29
  • 打赏
  • 举报
回复
上面错误


declare @flag char(1) ,@sql nvarchar(100)
select @flag='a'
select @sql='select a.id,a.name from test a where a.type in ( '+
( case @flag when 'a' then '''a'',''c'''
when 'b' then '''b'',''d'''
end )+')'
print @sql
execute sp_executesql @sql
sdhdy 2003-09-29
  • 打赏
  • 举报
回复
declare @flag char(1) --在运行时副职
if @flag='A'
select a.id,a.name from test a where a.type in ('a','c')
else
if @flag='B'
select a.id,a.name from test a where a.type in ('b','d')
aierong 2003-09-29
  • 打赏
  • 举报
回复
select a.id,a.name
from test a
where a.type in ( case @flag when 'A' then in('a','c')
when 'B' then in('b','d')
end )
txlicenhe 2003-09-29
  • 打赏
  • 举报
回复
if @flag = 'A'
select a.id,a.name
from test a
where a.type in ('a', 'c')
if @flag = 'B'
select a.id,a.name
from test a
where a.type in('a','c')
pengdali 2003-09-29
  • 打赏
  • 举报
回复
declare @flag char(1) --在运行时副职

select a.id,a.name
from test a
where a.type like case @flag when 'A' then '[ac]' when 'B' then '[bd]' end
LoveSQL 2003-09-29
  • 打赏
  • 举报
回复
and a.move_type = ( case @product when 'B' then 'decin' or'bomrtn' when 'A' then 'decout'or mkout' or 'prdmsy'or 'prdout' end ) --就是这里错误
========================================================================
把这句话改成下面这样,看看可以吗?
and a.move_type in(
select case @product when 'B' then 'decin' end union
select case @product when 'B' then 'bomrtn' end union
select case @product when 'A' then 'decout' end union
select case @product when 'A' then 'mkout' end union
select case @product when 'A' then 'prdmsy' end union
select case @product when 'A' then 'prdout' end )
bondhu 2003-09-29
  • 打赏
  • 举报
回复
不好意思,我将问题简化过度了,实际情况是这样的
CREATE PROCEDURE u_import_tritem
@dates datetime,
@datee datetime,
@movetype char(1), --in or out
@product char(1) --product or material

AS
select a.ref_no,a.move_date,a.tran_type,a.flow_type,a.move_type,a.issued_to,b.item_no,b.qty,
c.item_no,c.article_code,c.description
into #tt1
from icmovh a join icmovd b
on a.ref_no = b.ref_no join gbitem c on b.item_no = c.item_no

where a.move_date >= @dates and a.move_date <= @datee
and a.flow_type = ( case @product when 'A' then 'i' when 'B' then 'r'end )
and c.article_code is not null
and c.article_code <> ''
and a.posted = 'Y'
and a.move_type = ( case @product when 'B' then 'decin' or'bomrtn' when 'A' then 'decout'or mkout' or 'prdmsy'or 'prdout' end ) --就是这里错误

select * into #tt2 from dbo.trade e
where e.typeflag = @product

select a.ref_no,b.haiguanid,b.itemname,a.qty,c.oriprice,a.movedate,@movetype,@product
from #tt1 a join #tt2 b on a.article_code = b.article_code

当然还是那句case语句的问题。因为case在where 中所以楼上的部分方法用不上。因为a.move_type的值不子一个字符,所以大力的方法也用不上。aierong兄的应该可行,可不大方便,我再加100分,请兄台们想想如何解决。

提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!

22,209

社区成员

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

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