sql - WHERE CASE WHEN statement with Exists -
i creating sql query having where case when
statement. doing wrong , getting error.
my sql statement like
declare @areaid int = 2 declare @areas table(areaid int) insert @areas select areaid areamaster cityzoneid in (select cityzoneid areamaster areaid = @areaid) select * dbo.companymaster areaid in (case when exists (select businessid dbo.areasubscription areasubscription.businessid = companymaster.businessid) @areaid else (select [@areas].areaid @areas) end)
i getting error
msg 512, level 16, state 1, line 11
subquery returned more 1 value. not permitted when subquery follows =, !=, <, <= , >, >= or when subquery used expression.
please run query. logic checking conditional areaid
in (statement) each row.
i want select row when
- company has subscription entry
areasubscription
specific area passed@areaid
- table
areasubscription
not have subscription entry evaluateareaid
in(select [@areas].areaid @areas)
this may you.
select * dbo.companymaster areaid= (case when exists (select businessid dbo.areasubscription areasubscription.businessid = companymaster.businessid) @areaid else areaid end) , areaid in (select [@areas].areaid @areas)
one more solution is
select * dbo.companymaster left join @areas b on a.areaid=b.areaid a.areaid= (case when exists (select businessid dbo.areasubscription areasubscription.businessid = companymaster.businessid) @areaid else b.areaid end) )
Comments
Post a Comment