Saturday, May 31, 2014

COALESCE sql server

alter proc SP_DTV_SecBal
@division varchar(2), @ownerContact varchar(30)
AS
if @division='' begin set @division=null   end
if @ownerContact='' begin set @ownerContact=null   end



delete from Tbl_DTVSecBal
insert into Tbl_DTVSecBal
SELECT  eDAST, CAST(eDAN81 AS INT)eDAN81, LEFT(S.ABALPH, 25),
    ltrim(rtrim(T.WWGNNM)) + ' ' + ltrim(rtrim(T.WWSRNM)),
 '%' ,  CONVERT(MONEY, EDUNCS / 10000),
tg1.GregDay, tg2.GregDay,
    CAST(AICMC2 AS INT), LEFT(P.ABALPH, 25),
    ltrim(rtrim(U.WWGNNM)) + ' ' +  CASE when ltrim(rtrim(U.WWSRNM)) LIKE '%''%' THEN Replace(ltrim(rtrim(U.WWSRNM)), '''', '`') ELSE ltrim(rtrim(U.WWSRNM)) END
,p.abac19 Division,EDGLC,S.ABAT1,EDDCTO
FROM JDE_PRODUCTION.PRODDTA.F5542845 INNER JOIN JDE_PRODUCTION.PRODDTA.F0111 T
ON  EDAN81 = T.WWAN8  INNER JOIN vAB03012
ON  EDAN81 = AIAN8 INNER JOIN JDE_PRODUCTION.PRODDTA.F0111 U
ON  AICMC2 = U.WWAN8 INNER JOIN JDE_PRODUCTION.PRODDTA.F0101 S
ON  EDAN81 = S.ABAN8 INNER JOIN JDE_PRODUCTION.PRODDTA.F0101 P
ON  AICMC2 = P.ABAN8 INNER JOIN ZDEV.dbo.tblJulian_Gregorian tg1
ON  EDEFTB = tg1.JulianDay INNER JOIN ZDEV.dbo.tblJulian_Gregorian tg2
ON  EDEFTE=tg2.JulianDay --LEFT JOIN JDE_PRODUCTION.PRODDTA.F4094
WHERE EDAST IN ('DC1','DC2','DC3','DC4','DCNC','DCVP' )
AND (ltrim(rtrim(T.WWGNNM)) + ltrim(rtrim(T.WWSRNM)) != '')
AND (ltrim(rtrim(U.WWGNNM)) + ltrim(rtrim(U.WWSRNM)) != '')

--select * from Tbl_DTVSecBal
select distinct
AllOwnerID = z.AllOwnerID ,z.AlphaName,z.OwnerContact,z.division,
z.Adj,
[Ship To] = z.[Ship To] ,
z.SoldToCompany,
z.SalesPerson,
isnull(z.RateMode, '') RM,
cast(z.CommRate as decimal (15,2)) CommRate ,            
CONVERT(VARCHAR(10),z.Start,101)StartDate ,
CONVERT(VARCHAR(10),z.[End],101)EndDate ,Division,GLC,AT1,OrTY,
cast(v.SecBal as decimal (15,2)) SecBal
FROM Tbl_DTVSecBal z INNER JOIN  dbo.vSBL_SEC v ON z.[Ship To] = v.GBSBL
where z.AllOwnerID<>0
and z.division = COALESCE(@division, division) and  z.OwnerContact= = COALESCE(@OwnerContact,OwnerContact)


GO

No comments: