当SQL中的语句错误:列名称或所提供值的数量不匹配表定义时的情况
我将所有相关的内容都包括在错误之前,但是在案例语句代码之前,所有内容都可以正常运行。运行后,我会得到错误“列名称或所提供的值的数量不符合表定义”,但是我无法生存一生。谢谢!
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].
[Classifications]') AND TYPE IN (N'U'))
DROP TABLE Classifications
GO
CREATE TABLE Classifications
(
SegmentID nvarchar(10),
Waterbody_Type nvarchar(10),
Waterbody_Name nvarchar(100),
Basin nvarchar(50),
Segment_Miles int,
Water_Quality_Class nvarchar(100)
)
BULK INSERT Classifications FROM 'C:\Project\Classifications.csv'
WITH (
FIELDTERMINATOR = ',',
ROWTERMINATOR = '0x0a'
);
GO
ALTER TABLE Classifications ADD Segment_Length nvarchar(100)
DELETE FROM Classifications
WHERE COALESCE (Segment_Miles, Segment_Length) IS NULL;
INSERT INTO Classifications
SELECT SegmentID,
Segment_Length =
CASE
WHEN Segment_Miles >= 0 and Segment_Miles <= 50 THEN 'Short'
WHEN Segment_Miles > 50 and Segment_Miles <= 100 THEN 'Long'
ELSE '?'
END
FROM Classifications
I included everything relevant up until the error, but everything has worked and run fine prior to the case statement code; upon running I get the error "Column name or number of supplied values does not match table definition", but I cannot for the life of me find the issue. Thanks!
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].
[Classifications]') AND TYPE IN (N'U'))
DROP TABLE Classifications
GO
CREATE TABLE Classifications
(
SegmentID nvarchar(10),
Waterbody_Type nvarchar(10),
Waterbody_Name nvarchar(100),
Basin nvarchar(50),
Segment_Miles int,
Water_Quality_Class nvarchar(100)
)
BULK INSERT Classifications FROM 'C:\Project\Classifications.csv'
WITH (
FIELDTERMINATOR = ',',
ROWTERMINATOR = '0x0a'
);
GO
ALTER TABLE Classifications ADD Segment_Length nvarchar(100)
DELETE FROM Classifications
WHERE COALESCE (Segment_Miles, Segment_Length) IS NULL;
INSERT INTO Classifications
SELECT SegmentID,
Segment_Length =
CASE
WHEN Segment_Miles >= 0 and Segment_Miles <= 50 THEN 'Short'
WHEN Segment_Miles > 50 and Segment_Miles <= 100 THEN 'Long'
ELSE '?'
END
FROM Classifications
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您的意思是要进行更新,而不是最后插入。看来您正在尝试基于片段里程填充semgent_length字段,而不是仅添加sementID和semgent_length的新行。如果是这样 - 类似:
您遇到错误的原因是您试图将记录插入具有7个字段的表中,但是您的Select语句仅包含2个字段。如果您实际上是在尝试插入新记录,则需要更改插入行以指定您正在填充哪个字段:
I think you mean to be doing an update, not an insert at the end. It looks like you're trying to populate the Segment_Length field based on the Segment Miles, not add new rows with only the SegmentID and Segment_Length. If so - something like:
The reason you are getting the error is you are trying to insert records into a table with 7 fields, but your select statement contains only 2 fields. If you are actually trying to insert new records, then you need to change the INSERT line to specify which two fields you are populating: