Ok, this is what I am trying to achieve:
My script below is supposed to gather values across three tables and insert them in TO TABLE B in sequence.
There are two columns that are being affected in TABLE B the INDXLONG AND DDLINE.
IN The DDLINE ROW SHOULD I AM ATTEMPTING TO CONCATENATE VALUES FROM DIFFERENT FIELDS AND ADD THEM AS ONE.
I HAVE NARRATED BELOW.
MY GETTING ERRORS MAYBE MY LOGIC IS INCORRECT…SO PLEASE SHARE YOUR INSIGHT.
Declare
@nRowCountint,
@Indxlongint, @hdrLOCAL char(255),
@CRchar(255),
@BLDCHKDTDATETIME, @BLDCHTIMEDATETIME, @hdrline int,
@1strowlineint,
@2ndrowlineint,
@3rdrowlineint
@BWPchar(255),
@CompAccchar(11), @BankCodechar(11), @BranchNochar(11),
@PayDatedatetime,
@Referencechar(11), @TotaAmt numeric(19,5), @CoName char(11),
@BeneficiaryAccchar(11),
@BenBankBranchCode char(11),
@Salarynumeric (19,5),
@BeneficiaryName char(23),
@TransRefchar(23),
@outer_cint
SELECT@CompAcc=DDCOIDEN,
@BankCode =DDIMORIG,
@BranchNo =DDIMDEST,
@Reference =DDDESC10,
@CoName =DDIMORNM
FROMTABLE A
DeclareACHSCROLLCURSOR FOR
SELECTT762.DDINDNAM,
T762.DDTRANUM,
T762.DDACTNUM,
T762.DDAMTDLR,
T756.PAYDATE
FROMSTATS.dbo.TABLE CT762
LEFTOUTERJOINSTATS.dbo.TABLE DT756ON (
T762.INDXLONG=T756.INDXLONG
ANDT756.INCLPYMT= 1
)
WHERE (T756.INCLPYMT= 1)
AND(T762.DDAMTDLR<> 0)
FORREAD ONLY;
OPENACH;
SETNOCOUNTON;
FETCHFROMACHINTO @BeneficiaryName,@BenBankBranchCode,@BeneficiaryAcc,@Salary,@paydate
WHILE@@FETCH_STATUS= 0
BEGIN
Select@TotaAmt=SUM(@Salary)
set @hdrline=1
set@1strowline=2
set @2ndrowline=3
set @3rdrowline=9
SELECT @hdrLOCAL=DDLINEFROMTABLE E WHEREINDXLONG=1
SELECT @CR=DDLINEFROMTABLE E WHEREINDXLONG=2
SELECT @BWP=DDLINEFROMTABLE E WHEREINDXLONG=3
BEGIN
INSERTINTOTABLE B(INDXLONG,DDLINE)
VALUES (1,@hdrLOCAL+','+@CR+',')
SELECT@@IDENTITY
END
BEGIN
INSERTINTOTABLE B(INDXLONG,DDLINE)
VALUES (2,@CompAcc+@BranchNo+','+@BWP+ ','+@PayDate+','+@Reference+','+@TotaAmt+','+@TransRef)
SELECT@@IDENTITY
END
BEGIN
INSERTINTOTABLE B(INDXLONG,DDLINE)
VALUES (3,@BeneficiaryAcc+','+@BenBankBranchCode+','+@BeneficiaryAcc+','+ @Salary+','+@Reference+','+@TotaAmt+','+@TransRef)
SELECT@@IDENTITY
END
FETCHFROMACHINTO @BeneficiaryName,@BenBankBranchCode,@BeneficiaryAcc,@Salary,@paydate
END
CLOSEACH
DEALLOCATEACH
SETNOCOUNTOFF;
Msg 8152, Level 16, State 14, Line 69
String or binary data would be truncated.
The statement has been terminated.
Msg 241, Level 16, State 1, Line 74
Conversion failed when converting date and/or time from character string.
THIS IS THE RESULT I AM AIMING FOR:
INDXLONG DDLINE ----------- -----------------------------------------------------------------------
1 101001 029 1403200610A094101 AMEN BANK LOVE
2 123456 111 34567 PPDSALARYPAYT140131140117 11234567
3 63206623 0101962706200 0000062709000319614 ADAMS EVE
The Best thing in Life is Life