用一个SQL语句实现一个这种方法

funboy88 2003-05-23 06:02:20
用一个SQL语句实现一个这种方法


declare @typeid int

select * from tablename having (case when @typeid=1 then name='funboy' when @typeid=2 then sex='male' end)


想实现一个上面语句意思的功能
但这个语句是在MSSQL里通不过的,


功能描述:
从TABLENAME表查询数据,条件是根据@TYPEID值的不同,来给出不同的字段条件。

特别申明:不能用串SQL语句的方法,用EXEC执行它,

用一条SQL语句解决问题,不然没分啦!
...全文
60 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
pengdali 2003-05-24
  • 打赏
  • 举报
回复
declare @typeid int

select * from tablename where name=case @typeid when 1 then 'funboy' else name end and sex=case @typeid when 2 then 'male' else sex end
samuelpan 2003-05-24
  • 打赏
  • 举报
回复
OK::

select * from tablename where (@typeid=1 and name='funboy') or (@typeid=2 and sex='male')
playforfun 2003-05-24
  • 打赏
  • 举报
回复
存储过程不是支持条件判断的吗?

是不是不能用存储过程啊?

如果可以,那就很简单了。
yoki 2003-05-24
  • 打赏
  • 举报
回复
不知道这样算不算?
select * from tablename where typeid=1 and name='funboy'
union all
select * from tablename where typeid=2 and sex='male'

:)
liuyun2003 2003-05-24
  • 打赏
  • 举报
回复
MARK
funboy88 2003-05-24
  • 打赏
  • 举报
回复
yoki(小马哥)
samuelpan(欧乐)

你的答案也正确,
我要的是大力的那种效果的,实际的问题比这个更复杂
funboy88 2003-05-24
  • 打赏
  • 举报
回复
还是大力的力气大,厉害,问题得到解决
结贴

create table #
(
name varchar(100),
sex varchar(10)
)

insert into #(name,sex) values('funboy','female')
insert into #(name,sex) values('funboy1','female')
insert into #(name,sex) values('funboy','male')
insert into #(name,sex) values('funboy1','male')
insert into #(name,sex) values('funboy1','female')


declare @typeid int
set @typeid=1
select * from #
where name=(case @typeid when 1 then 'funboy' else name end )
and sex=(case @typeid when 2 then 'male' else sex end)

set @typeid=2
select * from #
where name=(case @typeid when 1 then 'funboy' else name end )
and sex=(case @typeid when 2 then 'male' else sex end)

drop table #
funboy88 2003-05-24
  • 打赏
  • 举报
回复
不知道大力的能不能通过

意思和我的差不多

是用SP的

但是不想用IF语句的

小马的那种UNION ALL行不通
longji 2003-05-23
  • 打赏
  • 举报
回复
declare @i integer
select @i=1
if @i=1
begin
select * from sales where qty=20
end
else
begin
select * from sales where qty=5
end
这样算不算啊?
HawaiiLeo 2003-05-23
  • 打赏
  • 举报
回复
不用 EXEC 俺是搞不定啦。
caiyunxia 2003-05-23
  • 打赏
  • 举报
回复
yoki 2003-05-23
  • 打赏
  • 举报
回复
还不能用动态语句?
去求神仙吧,估计它会
:)

22,210

社区成员

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

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