字符串或二进制数据将被截断。该语句已终止。 (长度相同)

时间:2016-02-01 19:03:54

标签: sql sql-server

我收到上面的错误消息,当你试图将较大的数据插入较小的列时,我知道它是“通常”。

问题是,进入的列不大于目的地。

看看:

CREATE TABLE #temp (
    row_id INT identity(1, 1)
    ,error_ind BIT DEFAULT 0
    ,add_edit_del_ind VARCHAR(1) DEFAULT ''
    ,vendor_sap_load_temp_id INT
    ,vendor_id VARCHAR(10)
    ,vendor_name VARCHAR(35)
    ,title VARCHAR(15)
    ,name2 VARCHAR(35)
    ,name3 VARCHAR(35)
    ,name4 VARCHAR(35)
    ,address VARCHAR(60)
    ,city VARCHAR(40)
    ,district VARCHAR(40)
    ,STATE VARCHAR(3)
    ,zipcode VARCHAR(10)
    ,country VARCHAR(3)
    ,account_grp VARCHAR(4)
    ,industry_key VARCHAR(4)
    ,ext_code VARCHAR(10)
    ,systems_ind VARCHAR(10)
    ,remit_to_vendor_id VARCHAR(10)
    ,LANGUAGE VARCHAR(10)
    ,individual_us_tax_id VARCHAR(9)
    ,vendor_us_tax_id VARCHAR(9)
    ,vat_reg_cd VARCHAR(20)
    ,tax_office VARCHAR(10)
    ,tax_nbr VARCHAR(18)
    ,tax_jurisdiction VARCHAR(15)
    ,phone_nbr VARCHAR(30)
    ,email_addr VARCHAR(241)
    ,delete_ind BIT
    ,stk_id INT
    ,stk_role_id INT
    ,name_id INT
    )

然后将数据插入此表中,这是所有列的MAX(LEN()):

SELECT max(len(vendor_id)) vendor_id
    ,max(len(vendor_name)) vendor_name
    ,max(len(title)) title
    ,max(len(name2)) name2
    ,max(len(name3)) name3
    ,max(len(name4)) name4
    ,max(len(address)) address
    ,max(len(city)) city
    ,max(len(district)) district
    ,max(len(STATE)) STATE
    ,max(len(zipcode)) zipcode
    ,max(len(country)) country
    ,max(len(account_grp)) account_grp
    ,max(len(industry_key)) industry_key
    ,max(len(ext_code)) ext_code
    ,max(len(systems_ind)) systems_ind
    ,max(len(remit_to_vendor_id)) remit_to_vendor_id
    ,max(len(LANGUAGE)) LANGUAGE
    ,max(len(individual_us_tax_id)) individual_us_tax_id
    ,max(len(vendor_us_tax_id)) vendor_us_tax_id
    ,max(len(vat_reg_cd)) vat_reg_cd
    ,max(len(tax_office)) tax_office
    ,max(len(tax_nbr)) tax_nbr
    ,max(len(tax_jurisdiction)) tax_jurisdiction
    ,max(len(phone_nbr)) phone_nbr
    ,max(len(email_addr)) email_addr
    ,max(len(delete_ind)) delete_ind
FROM #temp

结果:

这里我尝试插入目的地:

INSERT INTO final_destination_table (
    vendor_id
    ,vendor_name
    ,honorific
    ,name2
    ,name3
    ,name4
    ,address
    ,city
    ,district
    ,STATE
    ,zipcode
    ,country
    ,account_grp
    ,industry_key
    ,ext_code
    ,systems_ind
    ,remit_to_vendor_id
    ,LANGUAGE
    ,vendor_ssn
    ,vendor_tin
    ,vat_reg_cd
    ,tax_office
    ,tax_nbr
    ,tax_jurisdiction
    ,phone_nbr
    ,email_addr
    ,delete_ind
    )
SELECT vendor_id
    ,vendor_name
    ,title
    ,name2
    ,name3
    ,name4
    ,address
    ,city
    ,district
    ,STATE
    ,zipcode
    ,country
    ,account_grp
    ,industry_key
    ,ext_code
    ,systems_ind
    ,remit_to_vendor_id
    ,LANGUAGE
    ,individual_us_tax_id
    ,vendor_us_tax_id
    ,vat_reg_cd
    ,tax_office
    ,tax_nbr
    ,tax_jurisdiction
    ,phone_nbr
    ,email_addr
    ,delete_ind
FROM #temp

我收到错误:

这就是目标表的样子:

我没有看到列的长度有任何问题......有人能发现错误吗?

2 个答案:

答案 0 :(得分:7)

疯狂的猜测,但我打赌你在created_by列上有一个默认约束,它接受类似CURRENT_USER的东西,它的长度比created_by列的varchar(10)定义长。检查所有默认约束。

答案 1 :(得分:0)

解决问题的正确方法是找到导致错误的列并更正数据或列长度。

如果我们将ansi_warnings用作OFF,那么错误就是

。抑制以及列中的任何内容都会被插入,其余部分将被截断。

SET ansi_warnings OFF 
    GO
     INSERT INTO final_destination_table (
        vendor_id
        ,vendor_name
        ,honorific
        ,name2
        ,name3
        ,name4
        ,address
        ,city
        ,district
        ,STATE
        ,zipcode
        ,country
        ,account_grp
        ,industry_key
        ,ext_code
        ,systems_ind
        ,remit_to_vendor_id
        ,LANGUAGE
        ,vendor_ssn
        ,vendor_tin
        ,vat_reg_cd
        ,tax_office
        ,tax_nbr
        ,tax_jurisdiction
        ,phone_nbr
        ,email_addr
        ,delete_ind
        )
    SELECT vendor_id
        ,vendor_name
        ,title
        ,name2
        ,name3
        ,name4
        ,address
        ,city
        ,district
        ,STATE
        ,zipcode
        ,country
        ,account_grp
        ,industry_key
        ,ext_code
        ,systems_ind
        ,remit_to_vendor_id
        ,LANGUAGE
        ,individual_us_tax_id
        ,vendor_us_tax_id
        ,vat_reg_cd
        ,tax_office
        ,tax_nbr
        ,tax_jurisdiction
        ,phone_nbr
        ,email_addr
        ,delete_ind
    FROM #temp