简体   繁体   中英

Inserting concatenated values in T-SQL

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 I am attempting to concatenate values from different fields and to store them as one.

My code is below. Please share any insights:

Declare
    @nRowCount int, 
    @Indxlong int,
    @hdrLOCAL char(255),   
    @CR char(255),   
    @BLDCHKDT DATETIME,
    @BLDCHTIME DATETIME,   
    @hdrline int,   
    @1strowline int,   
    @2ndrowline int,   
    @3rdrowline int,   
    @BWP char(255),   
    @CompAcc char(11),   
    @BankCode char(11), 
    @BranchNo char(11),   
    @PayDate datetime,   
    @Reference char(11), 
    @TotaAmt numeric(19,5)
    @CoName char(11),   
    @BeneficiaryAcc char(11),   
    @BenBankBranchCode char(11),   
    @Salary numeric (19,5),   
    @BeneficiaryName char(23),   
    @TransRef char(23),   
    @outer_c int


SELECT @CompAcc =DDCOIDEN,
       @BankCode =DDIMORIG,
       @BranchNo =DDIMDEST,
       @Reference =DDDESC10,
       @CoName =DDIMORNM
FROM TABLE A

Declare ACH SCROLL CURSOR FOR 

SELECT T762.DDINDNAM,
        T762.DDTRANUM,
        T762.DDACTNUM,
        T762.DDAMTDLR,
        T756.PAYDATE
        FROM STATS.dbo.TABLE C T762
        LEFT OUTER JOIN STATS.dbo.TABLE D T756 ON (
                T762.INDXLONG = T756.INDXLONG
                AND T756.INCLPYMT = 1
                )
        WHERE (T756.INCLPYMT = 1)
            AND (T762.DDAMTDLR <> 0)
FOR READ ONLY;

OPEN ACH;
SET NOCOUNT ON;
FETCH FROM ACH INTO @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 = DDLINE FROM TABLE E WHERE INDXLONG =1
         SELECT @CR = DDLINE FROM TABLE E WHERE INDXLONG =2
         SELECT @BWP = DDLINE FROM TABLE E WHERE INDXLONG =3

BEGIN
    INSERT INTO TABLE B (INDXLONG,DDLINE)
    VALUES (1,@hdrLOCAL + ',' + @CR + ',' )
    SELECT @@IDENTITY
END
BEGIN
INSERT INTO TABLE B (INDXLONG,DDLINE)
    VALUES (2,@CompAcc + @BranchNo +','+ @BWP+ ',' + @PayDate +',' + @Reference + ','+@TotaAmt + ','+ @TransRef)
    SELECT @@IDENTITY
END

BEGIN
INSERT INTO TABLE B (INDXLONG,DDLINE)
VALUES (3,@BeneficiaryAcc + ',' + @BenBankBranchCode +','+ @BeneficiaryAcc+ ',' + @Salary +',' + @Reference + ','+@TotaAmt + ','+ @TransRef)
    SELECT @@IDENTITY
END
FETCH FROM ACH INTO @BeneficiaryName,@BenBankBranchCode,@BeneficiaryAcc,@Salary,@paydate
END

CLOSE ACH
DEALLOCATE ACH
SET NOCOUNT OFF;

This is the error:

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

Cast your dates into varchars/nvarchars if you are going to concatenate them. For examples, @PayDate should be casted like this: cast(PayDate as varchar(20)). Or if you need the date in a specific format, use Convert.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM