当我尝试在我的CUSTOMER_BILLING
表格中插入新行时,我一直收到错误"字符串或二进制数据会被截断。" 外国密钥是CUST_ID
,它来自CUSTOMER
表
CUSTOMER表:
INSERT INTO CUSTOMER
VALUES ('1234','Ragnar','Lothbrok','457 Raiding Rd','Kattegat','PA','19335','4845555555', 'rlothbrok@gmail.com)
INSERT INTO CUSTOMER
VALUES ('1235','Daenerys','Stormborn','567 Targaryen Way','Westoros','CA','90001','3026781234','bendtheknee@gmail.com')
INSERT INTO CUSTOMER
VALUES ('1236','Jon','Snow','123 Winterfell Ave','Castle Black','CA', '90002','6109258787', 'winteriscoming@gmail.com')
CUSTOMER_BILLING table
INSERT INTO CUSTOMER_BILLING
VALUES ('123456',(SELECT CUST_ID FROM CUSTOMER WHERE CUST_ID='1234'),'15-May-2017','14-Jun-2017','01-Jun-2017','567890','457 Raiding Rd','Kattegat','PA','19335','4845555555','rlothbrok@gmail.com')
我不确定外键是否有问题。任何帮助都会很棒。我使用的是Microsoft SQL 2017。
答案 0 :(得分:0)
试试这个:
INSERT INTO CUSTOMER
VALUES ('1234','Ragnar','Lothbrok','457 Raiding Rd','Kattegat','PA','19335','4845555555', 'rlothbrok@gmail.com')
INSERT INTO CUSTOMER
VALUES ('1235','Daenerys','Stormborn','567 Targaryen Way','Westoros','CA','90001','3026781234','bendtheknee@gmail.com')
INSERT INTO CUSTOMER
VALUES ('1236','Jon','Snow','123 Winterfell Ave','Castle Black','CA', '90002','6109258787', 'winteriscoming@gmail.com')
CUSTOMER_BILLING table
INSERT INTO CUSTOMER_BILLING
VALUES ('123456','1234','15-May-2017','14-Jun-2017','01-Jun-2017','567890','457 Raiding Rd','Kattegat','PA','19335','4845555555','rlothbrok@gmail.com')
答案 1 :(得分:0)
问题可能来自于追加查询的SELECT
子句中的嵌入式VALUES
,特别是因为此子查询返回了多条记录。
考虑使用INSERT INTO ... SELECT
追加查询,其中 CUST_ID 被检索,所有其他值都是标量。请注意,这将附加超过1条记录。如果不是这样,请使用DISTINCT
。
此外,为了避免任何可能与数据类型冲突的列混淆,请考虑通过省略列明确声明INSERT INTO
中的列而不是依赖列位置。
INSERT INTO CUSTOMER_BILLING (VarcharCol, VarcharCol, DateCol, DateCol, DateCol,
VarcharCol, VarcharCol, VarcharCol, VarcharCol,
VarcharCol, VarcharCol, VarcharCol)
SELECT '123456', CUST_ID, '15-May-2017', '14-Jun-2017', '01-Jun-2017',
'567890', '457 Raiding Rd', 'Kattegat', 'PA', '19335',
'4845555555', 'rlothbrok@gmail.com'
FROM CUSTOMER
WHERE CUST_ID = '1234'