数据库反范式设计中的查询

有时在设计数据库时,为了效率的考虑,不得不违反数据库的设计范式。这时在查询时可能遇到麻烦。此文就是为了解决此类问题的。

 此文为<<以"x1,x2,x3,x4"方式的查询>>( )的改进。

-- Gu Laicheng 2012.1.15
-- 在F0001中查找F0002,如找到则返回"1",否则返回"0"
-- 如果F0001="_ALL_",表示所有,则返回1
alter function gSeek(@F0001 varchar(200),@F0002 varchar(200))
returns bit
as
begin
declare @ret bit
 select @ret=
 case
  when @F0001='_ALL_' then 'true'
  when (patindex('%|'+rtrim(@F0002)+'|%','|'+replace(rtrim(@F0001),',','|')+'|')>0) then 'true'
  else 'false'
 end
 return @ret
end

 

declare @F0001 varchar(200)
declare @F0002 varchar(200)

select @F0001='AA,CC,DD'
select @F0002='BB'


select
case
when @F0001='_ALL_' then 'true'
when (patindex('%|'+rtrim(@F0002)+'|%','|'+replace(rtrim(@F0001),',','|')+'|')>0) then 'true'
else 'false'
end

select dbo.gSeek(@F0001,@F0002)

 

select * from dbo.Table1


id   a    b              c
1    2    2,3,4       4
2    5    _ALL_     5
3    4    5,6,7       6
4    3    1,2,3       2

select * from dbo.Table1 where dbo.gSeek(b,a)=1

id     a     b             c
1      2     2,3,4      4
2      5   _ALL_      5
4      3    1,2,3        2

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值