gpt4 book ai didi

sql - 插入时出现 TSQL 错误 "String or binary data would be truncated"

转载 作者:行者123 更新时间:2023-12-02 11:57:01 26 4
gpt4 key购买 nike

在下面的代码中,我将值插入表中并收到错误“字符串或二进制数据将被截断。”

我的表定义:

CREATE TABLE urs_prem_feed_out_control
(
bd_pr_cntl_rec_type char(7) NULL ,
pd_pr_cntl_acctg_dte char(6) NULL ,
bd_pr_cntl_run_dte char(10) NULL ,
bd_pr_cntl_start_dte char(10) NULL ,
bd_pr_cntl_end_dte char(10) NULL ,
bd_pr_cntl_rec_count char(16) NULL ,
bd_pr_tot_premium char(16) NULL ,
bd_pr_tot_commission char(16) NULL ,
fd_ctl_nbr integer NOT NULL
)

DECLARE @cur_fd_ctl_nbr INT = 2,
@acctg_cyc_ym_2 CHAR(6) = '201402',
@rundate CHAR (10) = CONVERT(CHAR(10),GETDATE(),101),
@cycle_start_dt DATETIME = '2014-02-17',
@cycle_end_dt DATETIME = '2014-02-24',
@record_count INT = 24704,
@tot_pr_premium DECIMAL(18,2) = 476922242,
@tot_pr_comm DECIMAL(18,2) = 2624209257

插入代码(我已将变量声明为常量值以进行测试,我从运行时获取这些值):

INSERT INTO urs_prem_feed_out_control
SELECT fd_ctl_nbr = @cur_fd_ctl_nbr,
bd_pr_cntl_rec_type = 'CONTROL',
bd_pr_cntl_acctg_dte = @acctg_cyc_ym_2,
bd_pr_cntl_run_dte = @rundate,
bd_pr_cntl_start_dte = CONVERT(CHAR(10),@cycle_start_dt,101),
bd_pr_cntl_end_dte = CONVERT(CHAR(10),@cycle_end_dt,101),
bd_pr_cntl_rec_count = RIGHT('0000000000000000' + RTRIM(CONVERT(CHAR(16),@record_count)),16),
bd_pr_tot_premium = CASE
WHEN @tot_pr_premium < 0
THEN '-' + SUBSTRING(RIGHT('000000000000000' + LTRIM(RTRIM(CONVERT(VARCHAR,ABS(@tot_pr_premium)*100))),18),1,15)
ELSE
'+' + SUBSTRING(RIGHT('000000000000000' + LTRIM(RTRIM(CONVERT(VARCHAR,ABS(@tot_pr_premium)*100))),18),1,15)
END,
bd_pr_tot_commission = CASE
WHEN @tot_pr_comm < 0
THEN '-' + SUBSTRING(RIGHT('000000000000000' + LTRIM(RTRIM(CONVERT(VARCHAR,ABS(@tot_pr_comm)*100))),18),1,15)
ELSE
'+' + SUBSTRING(RIGHT('000000000000000' + LTRIM(RTRIM(CONVERT(VARCHAR,ABS(@tot_pr_comm)*100))),18),1,15)
END

当我单独查看每个值时,它们似乎都在表的可变长度限制内。知道为什么我会收到此错误吗?

谢谢!

最佳答案

插入查询的问题是插入顺序:

SELECT fd_ctl_nbr = @cur_fd_ctl_nbr,

此列必须在 INSERT 的最后定义,因为它是在创建表脚本中定义的最后一列。

将您的查询更改为:

   INSERT INTO #urs_prem_feed_out_control (fd_ctl_nbr, bd_pr_cntl_rec_type, pd_pr_cntl_acctg_dte, bd_pr_cntl_run_dte, bd_pr_cntl_start_dte, bd_pr_cntl_end_dte, bd_pr_cntl_rec_count, bd_pr_tot_premium, bd_pr_tot_commission)
SELECT fd_ctl_nbr = @cur_fd_ctl_nbr,
bd_pr_cntl_rec_type = 'CONTROL',
bd_pr_cntl_acctg_dte = @acctg_cyc_ym_2,
bd_pr_cntl_run_dte = @rundate,
bd_pr_cntl_start_dte = CONVERT(CHAR(10),@cycle_start_dt,101),
bd_pr_cntl_end_dte = CONVERT(CHAR(10),@cycle_end_dt,101),
bd_pr_cntl_rec_count = RIGHT('0000000000000000' + RTRIM(CONVERT(CHAR(16),@record_count)),16),
bd_pr_tot_premium = CASE
WHEN @tot_pr_premium < 0
THEN '-' + SUBSTRING(RIGHT('000000000000000' + LTRIM(RTRIM(CONVERT(VARCHAR,ABS(@tot_pr_premium)*100))),18),1,15)
ELSE
'+' + SUBSTRING(RIGHT('000000000000000' + LTRIM(RTRIM(CONVERT(VARCHAR,ABS(@tot_pr_premium)*100))),18),1,15)
END,
bd_pr_tot_commission = CASE
WHEN @tot_pr_comm < 0
THEN '-' + SUBSTRING(RIGHT('000000000000000' + LTRIM(RTRIM(CONVERT(VARCHAR,ABS(@tot_pr_comm)*100))),18),1,15)
ELSE
'+' + SUBSTRING(RIGHT('000000000000000' + LTRIM(RTRIM(CONVERT(VARCHAR,ABS(@tot_pr_comm)*100))),18),1,15)
END

这样做也可以。请注意,INSERTSELECT 中的第一列正是您在问题中提供的方式。

请参阅此处-> http://sqlfiddle.com/#!3/0e09b/1

希望这有帮助!!!

关于sql - 插入时出现 TSQL 错误 "String or binary data would be truncated",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24372512/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com