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

  1. company has subscription entry areasubscription specific area passed @areaid
  2. table areasubscription not have subscription entry evaluate areaid 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

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -