[dbo].[OrderPhoneNumbers]
SET QUOTED_IDENTIFIER OFF
GO
CREATE view [dbo].[OrderPhoneNumbers]
(
OrderFID,
OriginHomeCountryCodeStandardFID,
OriginHomeAreaCode,
OriginHomeLocalNumber,
OriginHomePhoneNumber,
OriginWorkCountryCodeStandardFID,
OriginWorkAreaCode,
OriginWorkLocalNumber,
OriginWorkPhoneNumber,
OriginWorkExtension,
DestinationHomeCountryCodeStandardFID,
DestinationHomeAreaCode,
DestinationHomeLocalNumber,
DestinationHomePhoneNumber,
DestinationWorkCountryCodeStandardFID,
DestinationWorkAreaCode,
DestinationWorkLocalNumber,
DestinationWorkPhoneNumber,
DestinationWorkExtension
)
as
select
OrderFID = Orders.PriKey,
OriginHomeCountryCodeStandardFID = ohPhoneNumber.CountryCodeStandardFID,
OriginHomeAreaCode = ohPhoneNumber.AreaCode,
OriginHomeLocalNumber = ohPhoneNumber.LocalNumber,
OriginHomePhoneNumber = dbo.GetFormattedPhoneNumber( ohPhoneNumber.CountryCodeStandardFID, ohPhoneNumber.AreaCode, ohPhoneNumber.LocalNumber ),
OriginWorkCountryCodeStandardFID = owPhoneNumber.CountryCodeStandardFID,
OriginWorkAreaCode = owPhoneNumber.AreaCode,
OriginWorkLocalNumber = owPhoneNumber.LocalNumber,
OriginWorkPhoneNumber = dbo.GetFormattedPhoneNumber( owPhoneNumber.CountryCodeStandardFID, owPhoneNumber.AreaCode, owPhoneNumber.LocalNumber ),
OriginWorkExtension = owPhoneNumber.Extension,
DestinationHomeCountryCodeStandardFID =
case
when mhPhoneNumber.OrderPhoneNumberID is null then dhPhoneNumber.CountryCodeStandardFID
else mhPhoneNumber.CountryCodeStandardFID
end,
DestinationHomeAreaCode =
case
when mhPhoneNumber.OrderPhoneNumberID is null then dhPhoneNumber.AreaCode
else mhPhoneNumber.AreaCode
end,
DestinationHomeLocalNumber =
case
when mhPhoneNumber.OrderPhoneNumberID is null then dhPhoneNumber.LocalNumber
else mhPhoneNumber.LocalNumber
end,
DestinationHomePhoneNumber =
case
when mhPhoneNumber.OrderPhoneNumberID is null
then dbo.GetFormattedPhoneNumber( dhPhoneNumber.CountryCodeStandardFID, dhPhoneNumber.AreaCode, dhPhoneNumber.LocalNumber )
else dbo.GetFormattedPhoneNumber( mhPhoneNumber.CountryCodeStandardFID, mhPhoneNumber.AreaCode, mhPhoneNumber.LocalNumber )
end,
DestinationWorkCountryCodeStandardFID =
case
when mwPhoneNumber.OrderPhoneNumberID is null then dwPhoneNumber.CountryCodeStandardFID
else mwPhoneNumber.CountryCodeStandardFID
end,
DestinationWorkAreaCode =
case
when mwPhoneNumber.OrderPhoneNumberID is null then dwPhoneNumber.AreaCode
else mwPhoneNumber.AreaCode
end,
DestinationWorkLocalNumber =
case
when mwPhoneNumber.OrderPhoneNumberID is null then dwPhoneNumber.LocalNumber
else mwPhoneNumber.LocalNumber
end,
DestinationWorkPhoneNumber =
case
when mwPhoneNumber.OrderPhoneNumberID is null
then dbo.GetFormattedPhoneNumber( dwPhoneNumber.CountryCodeStandardFID, dwPhoneNumber.AreaCode, dwPhoneNumber.LocalNumber )
else dbo.GetFormattedPhoneNumber( mwPhoneNumber.CountryCodeStandardFID, mwPhoneNumber.AreaCode, mwPhoneNumber.LocalNumber )
end,
DestinationWorkExtension =
case
when mwPhoneNumber.OrderPhoneNumberID is null
then dwPhoneNumber.Extension
else mwPhoneNumber.Extension
end
from Orders
left outer join OrderPhoneType ohPhoneType on ( ohPhoneType.TypeName = 'Origin Home' )
left outer join OrderPhoneNumber ohPhoneNumber on ( ohPhoneNumber.OrderFID = Orders.Prikey and ohPhoneNumber.OrderPhoneTypeFID = ohPhoneType.OrderPhoneTypeID )
left outer join OrderPhoneType owPhoneType on ( owPhoneType.TypeName = 'Origin Work' )
left outer join OrderPhoneNumber owPhoneNumber on ( owPhoneNumber.OrderFID = Orders.Prikey and owPhoneNumber.OrderPhoneTypeFID = owPhoneType.OrderPhoneTypeID )
left outer join OrderPhoneType dhPhoneType on ( dhPhoneType.TypeName = 'Destination Home' )
left outer join OrderPhoneNumber dhPhoneNumber on ( dhPhoneNumber.OrderFID = Orders.Prikey and dhPhoneNumber.OrderPhoneTypeFID = dhPhoneType.OrderPhoneTypeID )
left outer join OrderPhoneType dwPhoneType on ( dwPhoneType.TypeName = 'Destination Work' )
left outer join OrderPhoneNumber dwPhoneNumber on ( dwPhoneNumber.OrderFID = Orders.Prikey and dwPhoneNumber.OrderPhoneTypeFID = dwPhoneType.OrderPhoneTypeID )
left outer join OrderPhoneType mhPhoneType on ( mhPhoneType.TypeName = 'Moving To Home' )
left outer join OrderPhoneNumber mhPhoneNumber on ( mhPhoneNumber.OrderFID = Orders.Prikey and mhPhoneNumber.OrderPhoneTypeFID = mhPhoneType.OrderPhoneTypeID )
left outer join OrderPhoneType mwPhoneType on ( mwPhoneType.TypeName = 'Moving To Work' )
left outer join OrderPhoneNumber mwPhoneNumber on ( mwPhoneNumber.OrderFID = Orders.Prikey and mwPhoneNumber.OrderPhoneTypeFID = mwPhoneType.OrderPhoneTypeID )
GO
GRANT SELECT ON [dbo].[OrderPhoneNumbers] TO [MssExec]
GRANT INSERT ON [dbo].[OrderPhoneNumbers] TO [MssExec]
GRANT DELETE ON [dbo].[OrderPhoneNumbers] TO [MssExec]
GRANT UPDATE ON [dbo].[OrderPhoneNumbers] TO [MssExec]
GO