VBA / SQL,无效使用Null

时间:2018-05-18 15:40:59

标签: sql excel ms-access access-vba

我在excel中使用userform在我的电子表格之间运行一些内部查询。 但是我得到Invalid Use of Null并且我知道nz null语法(SQL MS Access - Invalid Use of Null)但是我的查询可能非常大,我想知道是否有什么我可以添加到我的vba允许空值的代码。

这是我的查询,

select [EUC_ID],
RiskReportProductType,
[Source Currency of the CUSIP that is denominated in],
[EnterpriseCurrency],[EnterpriseSector],
[RatingCombo],
iif(RiskReportProductType in ('Equity|Private','Equity|Public','Seed Capital','Pref Shares','GLadjustment','OtherAdjustment'),'n/a',
switch(
[EnterpriseRating] = 'No Rating','BBB',
[EnterpriseRating] <> 'No Rating',[EnterpriseRating],
[EnterpriseRating] is null,'Unmapped - no rating mapping for ' & [RatingCombo],
)) as [EnterpriseRating],
[Issuer],

iif(RiskReportProductType in ('Bond|Corporate','Hybrid','CMBS|CAD','MBS|CAD','Loan Substitute securities'),
switch(
[EnterpriseCurrency] is null, 'Unmapped - no currency mapping for ' & [Source Currency of the CUSIP that is denominated in],
[EnterpriseRating] is null,'Unmapped - no rating mapping for ' & [RatingCombo],
[EnterpriseSector] is null,'Unmapped - no sector mapping for ' & [BbgSector],
[EnterpriseCurrency] is not null and [EnterpriseRating] is not null and [EnterpriseSector] is not null,'CORPZERO'&'|'&[EnterpriseCurrency]&'|'&[EnterpriseRating]&'|'&[EnterpriseSector],
true,'Unmapped'
),'n/a') as [CorporateBond_DiscountCurve],

iif(RiskReportProductType in ('Bond|Sovereign'),
switch(
[Risk free curve] is null, 'Unmapped - no risk free curve for ' & [Source Currency of the CUSIP that is denominated in] & '|' & [EnterpriseCurrency],
[Risk free curve] is not null, [Risk free curve],
true, 'Unmapped'
),'n/a') as [SovereignBond_DiscountCurve],

iif(RiskReportProductType in ('Bond|Agency','Bond|Special Purpose Entity'),
switch(
[EnterpriseRating] is null,'Unmapped - no rating mapping for ' & [RatingCombo],
[EnterpriseRating] is not null, 'CORPZERO'&'|'&[EnterpriseCurrency]&'|'&[EnterpriseRating]&'|'&'AGN',
true,'Unmapped'
),'n/a') as [AgencyBond_DiscountCurve],

iif(RiskReportProductType in ('Bond|Regional Authority'),
switch(
[Province] is null,'Unmapped - no issuer mapping for ' & [Issuer],
[EnterpriseCurrency] is null, 'Unmapped - no currency mapping for ' & [Source Currency of the CUSIP that is denominated in],
[EnterpriseRating] is null, 'Unmapped - no rating mapping for ' & [RatingCombo],
[Province]='AGN', 'CORPZERO'&'|'&[EnterpriseCurrency]&'|'&[EnterpriseRating]&'|'&'AGN',
[EnterpriseCurrency] in ('USD','CAD'),'PROVZERO'&'|'&[EnterpriseCurrency]&'|'&[Province]&'|'&[EnterpriseRating],
true,'Unmapped'
),'n/a') as [RegionalAuthorityBond_DiscountCurve],

iif(RiskReportProductType in ('Bond|Supra'),
switch(
[SupraCurve] is null,'Unmapped - no supra mapping for ' & [Issuer],
[EnterpriseRating] is null, 'Unmapped - no rating mapping for ' & [RatingCombo],
[SupraCurve] is not null and [EnterpriseRating] is not null, [SupraCurve]&'|'&iif([EnterpriseRating]='No Rating','B',[EnterpriseRating])&'|'&'SUP',
true,'Unmapped'
),'n/a') as [SupraBond_DiscountCurve],

iif(RiskReportProductType in ('Bond|Muni'),
switch(
[MuniRating] is null, 'Unmapped - no muni rating mapping for ' & [EnterpriseRating],
[MuniRating] is not null, 'MUNIZERO'&'|'&'USD'&'|'&[MuniRating]&'|'&'MID',
true, 'UnMapped'
),'n/a') as [MuniBond_DiscountCurve],

iif(RiskReportProductType in ('ABS','ARS','CLO','CMBS|USD','MBS|USD','MBS|GBP','MBS|CMO|Agency|USD','MBS|CMO|USD','MBS|Agency|USD'),
'SWAPZERO'&'|'&[EnterpriseCurrency]&'|'&'|MID','n/a') as [LIBORPRODUCTS_DiscountCurve],

switch(
[RiskSource] = 'No risk','n/a',
[LeafNodeName] is null,'Unmapped - no leaf node for transit '& [Transit # (not rollup)],
[LeafNodeName] is not null, [LeafNodeName]) as [EnterpriseNode],
PolyMaturity,
null as Combined_DiscountCurve

from (
select distinct
master.[EUC_ID],map_rate.[EnterpriseRating],
master.[Moodys External Rating#  Input NR if the security is not rated o]&'|'&
master.[S&P External Rating#  Input NR if the security is not rated or W]&'|'&
master.[Fitch External Rating  Input NR if the security is not rated or ] as RatingCombo,map_poly.RiskReportProductType,map_poly.PolyMaturity,
map_risksource.RiskSource,map_curr.[EnterpriseCurrency],map_prod.[EnterpriseSector],
map_supracurr.[SupraCurve],map_munirate.[MuniRating],map_rfcurve.[Risk free curve],
map_issuer.[Province],map_transit.[Transit],map_transit.LeafNodeName,master.[Source Currency of the CUSIP that is denominated in],
map_prod.[BbgSector],master.[Issuer],master.[Transit # (not rollup)]
from
((((((((((
(select *
from [masterbankingbookdb$]) as master
left join 
(select [FinanceDatabaseRatingCombo],[EnterpriseRating] from [mappings$]
where [EnterpriseRating] is not null
) as map_rate
on map_rate.[FinanceDatabaseRatingCombo] =
(master.[Moodys External Rating#  Input NR if the security is not rated o]&'|'&
master.[S&P External Rating#  Input NR if the security is not rated or W]&'|'&
master.[Fitch External Rating  Input NR if the security is not rated or ] 
))
left join 
(select [poly_EUC_ID],RiskReportProductType,PolyMaturity from [mapped_polytypes$]
where [poly_EUC_ID] is not null
) as map_poly
on map_poly.poly_euc_id=master.euc_id)
left join 
(select [EUC_ID],risksource from [mapped_risksource$]
where [EUC_ID] is not null
) as map_risksource
on map_risksource.euc_id=master.euc_id)
left join 
(select [FinanceCurrency],[EnterpriseCurrency] from [mappings$] where [FinanceCurrency] is not null
) as map_curr
on map_curr.FinanceCurrency=master.[Source Currency of the CUSIP that is denominated in])
left join 
(select [euc_id],[BbgType_IndustrySector] from [mapped_product$] where [euc_id] is not null
) as bbg_prod
on bbg_prod.euc_id=master.euc_id)
left join 
(select [BbgSector],[EnterpriseSector] from [mappings$] where [BbgSector] is not null
) as map_prod on map_prod.[BbgSector]=iif(bbg_prod.[BbgType_IndustrySector] is null,'All',bbg_prod.[BbgType_IndustrySector])
)
left join 
(select [SupraCurrency],[SupraCurve] from [mappings$] where [SupraCurrency] is not null
) as map_supracurr
on map_supracurr.SupraCurrency=master.[Source Currency of the CUSIP that is denominated in])
left join 
(select [CorpRating],[MuniRating] from [mappings$] where [CorpRating] is not null
) as map_munirate
on map_munirate.[CorpRating]=map_rate.[EnterpriseRating])
left join 
(select [Currency],[Risk free curve] from [mappings$] where [Currency] is not null
) as map_rfcurve
on cstr(map_rfcurve.[Currency])=cstr(map_curr.[EnterpriseCurrency]))
left join 
(select [Issuer],[Province] from [mappings$] where [Issuer] is not null
) as map_issuer
on iif(map_issuer.[Issuer] is not null,map_issuer.[Issuer],'All')=iif(master.[Issuer] is null,'All',master.[Issuer]))
left join 
(select [Transit],[LeafNodeName] from [mappings$] where [Transit] is not null
) as map_transit
on cstr(map_transit.[Transit])=cstr(master.[Transit # (not rollup)])
) as X

1 个答案:

答案 0 :(得分:10)

强制转换函数(如CStr)非常容易触发无效使用null 。不应该在连接中使用CStr,因为它会为了获得相等而进行类型转换。

删除以下位置的CStr

on cstr(map_transit.[Transit])=cstr(master.[Transit # (not rollup)])

on cstr(map_rfcurve.[Currency])=cstr(map_curr.[EnterpriseCurrency]))

请注意,虽然您已在这些子查询中使用Is Not Null进行过滤,但优化程序可能会首先执行连接,然后在过滤之前抛出Invalid Use Of Null。有关优化程序执行类似操作的示例案例,请参阅this answer

或者,如果您真的想要转换为字符串,可以使用以下内容:

on (map_transit.[Transit] & '' = master.[Transit # (not rollup)] & '')

将一个空字符串附加到某个字符串会将其值转换为字符串,即使它是空的,也不会抛出错误。