delete from tbl_CRM_FiscalCalendar where ownerId < 0; delete from tbl_CRM_FiscalCalendar where ownerType = 'BRO' and not exists ( select * from tblBrokers where broker_id =ownerId ) and ownerId > 0; delete from tbl_CRM_FiscalCalendar where ownerType = 'MFR' and not exists ( select * from tblManufacturers where mfr_id =ownerId ) and ownerId > 0; delete from tbl_CRM_FiscalCalendar where ownerType = 'DST' and not exists ( select * from tbl_DST_Distributors where dstid = ownerId ) and ownerId > 0; delete from tbl_CRM_FiscalCalendar where ownerType = 'CHO' and not exists ( select * from tbl_CHO_Operators where cho_id = ownerId ) and ownerId > 0; select fc.ownerId, fc.ownertype, fc.fiscalYear from tbl_CRM_FiscalCalendar fc with (nolock) where not exists ( select * from tbl_CRM_FiscalPeriods fp where fp.ownerId =fc.ownerId and fp.ownerType = fc.ownerType and fp.fiscalYear = fc.fiscalYear ) and ownerId > 0 and fiscalYear >= 2007 and fiscalYear <= Year( getDate() ) + 3 order by ownerType, ownerId, fiscalYear

Issues with #ownerType#:#ownerId#