在T-SQL中插入连接值

时间:2014-03-20 20:08:28

标签: sql tsql

好的,这就是我想要实现的目标:

我的下面的脚本应该在三个表中收集值并按顺序将它们插入到TO B中。

表B中有两列受影响的INDXLONG和DDLINE。

在DDLINE行中,我试图连接来自不同字段的值并将它们存储为一个。

我的代码如下。请分享任何见解:

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;

这是错误:

  

Msg 8152,Level 16,State 14,Line 69
  字符串或二进制数据将被截断   声明已经终止   消息241,级别16,状态1,行74   从字符串转换日期和/或时间时转换失败。

这是我的目标:

INDXLONG    DDLINE                                                                                                                                                                                                                                                         ----------- -----------------------------------------------------------------------
1           101001       029       1403200610A094101 AMEN  BANK          LOVE    
2           123456 111               34567   PPDSALARYPAYT140131140117   11234567 
3           63206623    0101962706200    0000062709000319614      ADAMS EVE          

1 个答案:

答案 0 :(得分:0)

如果要连接它们,请将日期转换为varchars / nvarchars。例如,@ PayDate应该像这样:cast(PayDate as varchar(20))。或者,如果您需要特定格式的日期,请使用转换。