当SQL中的语句错误:列名称或所提供值的数量不匹配表定义时的情况

发布于 2025-01-25 17:30:44 字数 1094 浏览 3 评论 0原文

我将所有相关的内容都包括在错误之前,但是在案例语句代码之前,所有内容都可以正常运行。运行后,我会得到错误“列名称或所提供的值的数量不符合表定义”,但是我无法生存一生。谢谢!

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谜兔 2025-02-01 17:30:44

我认为您的意思是要进行更新,而不是最后插入。看来您正在尝试基于片段里程填充semgent_length字段,而不是仅添加sementID和semgent_length的新行。如果是这样 - 类似:

UPDATE Classifications
   SET 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

您遇到错误的原因是您试图将记录插入具有7个字段的表中,但是您的Select语句仅包含2个字段。如果您实际上是在尝试插入新记录,则需要更改插入行以指定您正在填充哪个字段:

INSERT INTO Classifications (SegmentID, Segment_Length) 
...

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:

UPDATE Classifications
   SET 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

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:

INSERT INTO Classifications (SegmentID, Segment_Length) 
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文