-- method 1 : you can do this by customerID instead of BillCode (because there is no diff in prefix)
SELECT distinct @Company_Code,
--CustomerID,
'bc'+ cast(row_number()over(order by customerID) as varchar)as BillCode,
AddressLine1,
AddressLine2,
City_Suburb,
StateAbbreviation,
Zip_Postcode,
CC.Country_Code,
Phone ,
Phone as phone_ext,
Email,
'' as Fax,
GETDATE() as create_date_time,
'SYSTEM' as create_user_id,
GETDATE() as last_update_date_time,
'SYSTEM' as last_update_user_id
from MTrackDataImport..rawCustomerSISU C
LEFT OUTER JOIN CD_country CC
ON (CC.Company_Code = @Company_Code
AND C.Country = CC.Country_Name)
order by customerID
-- method 2 : you can do this by common table expression CTE
;with CTE as (SELECT distinct @Company_Code,
--CustomerID,
'bc'+ cast(row_number()over(order by customerID) as varchar)as BillCode,
AddressLine1,
AddressLine2,
City_Suburb,
StateAbbreviation,
Zip_Postcode,
CC.Country_Code,
Phone ,
Phone as phone_ext,
Email,
'' as Fax,
GETDATE() as create_date_time,
'SYSTEM' as create_user_id,
GETDATE() as last_update_date_time,
'SYSTEM' as last_update_user_id
from MTrackDataImport..rawCustomerSISU C
LEFT OUTER JOIN CD_country CC
ON (CC.Company_Code = @Company_Code
AND C.Country = CC.Country_Name)
)
select * from cte order by BillCode