如何解决SQL Server中的错误INSERT INTO?

时间:2018-03-14 16:36:23

标签: sql sql-server sql-insert

这是我的问题:

INSERT INTO aa_Tanks_Ponds ([User_Name],
                            [Type],
                            Site_Name,
                            Species,
                            Year_Class,
                            Tank_Pond,
                            Letter,
                            [Name],
                            Tank_Pond_uID,
                            Start_Inventory_#,
                            Start_Inventory_lbs,
                            Start_Inventory_avg,
                            Stocking_#,
                            Stocking_lbs,
                            Stocking_from,
                            Stocking_Total_#,
                            Stocking_Total_lbs,
                            Sales_#,
                            Sales_lbs,
                            Sales_Total_#,
                            Sales_Total_lbs,
                            Mortality_#,
                            Mortality_lbs,
                            Transfers_#,
                            Transfers_lbs,
                            Transfers_To,
                            Transfer_Total_#,
                            Transfer_Total_lbs,
                            Plus_Min_#,
                            Plus_Min_lbs,
                            Plus_Min_Total_#,
                            Plus_Min_Total_lbs,
                            Feed_lbs_Tanks_Ponds,
                            Feed_growth_feed,
                            Feed_growth_FCR,
                            Feed_growth_gain,
                            Final_Inventory_#,
                            Final_Inventory_lbs,
                            Final_Inventory_avg,
                            bg_color,
                            tx_color,
                            sort_order,
                            FeedValue)
VALUES ('Chase Ayers','Chase Ayers','Chase Ayers','Chase Ayers','Chase Ayers','Chase Ayers'),
       (NULL,NULL,NULL,NULL,NULL,NULL),
       ('Dry Creek','Dry Creek','Dry Creek','Dry Creek','Dry Creek','Dry Creek'),
       ('Sturgeon','Sturgeon','Sturgeon','Sturgeon','Sturgeon','Sturgeon'),
       ('14C','14C','14C','14C','14C','14C'),
       ('Tank','Tank','Tank','Tank','Tank','Tank'),
       ('H','H','H','H','H','H'),
       ('H1','H2','H3','H4','H5','H6'),
       ('DCH1','DCH2','DCH3','DCH4','DCH5','DCH6'),
       (2985,2995,2678,2947,3175,3040),
       (30144,27560,27161,27956,32600,33221),
       (10.1,9.2,10.14,9.49,10.27,10.93),
       (0,0,0,0,0,0),
       (0,0,0,0,0,0),
       (' ',' ',' ',' ',' ',' '),
       (0,0,0,0,0,0),
       (0,0,0,0,0,0),
       (0,0,0,0,0,0),
       (0,0,0,0,0,0),
       (0,0,0,0,0,0),
       (0,0,0,0,0,0),
       (0,0,0,0,0,0),
       (0,0,0,0,0,0),
       (0,0,0,0,0,0),
       (0,0,0,0,0,0),
       (' ',' ',' ',' ',' ',' '),
       (0,0,0,0,0,0),
       (0,0,0,0,0,0),
       (0,0,0,0,0,0),
       (0,0,0,0,0,0),
       (0,0,0,0,0,0),
       (0,0,0,0,0,0),
       (0,0,0,0,0,0),
       (0,0,0,0,0,0),
       (2,2,2,2,2,2),
       (0,0,0,0,0,0),
       (2985,2995,2678,2947,3175,3040),
       (30144,27560,27161,27956,32600,33221),
       (10.1,9.2,10.14,9.49,10.27,10.93),
       ('FFFF00','99CC33','3399CC','FF66CC','FFFF00','FFFF00'),
       ('996600','CCFF66','660099','CCCCCC','900000','FFCC99'),
       (0,0,0,0,0,0),
       (NULL,NULL,NULL,NULL,NULL,NULL);

我经过多次检查,试图找出错误的原因:

  

INSERT语句中的列数多于指定的值   在VALUES条款中。 VALUES子句中的值的数量必须为   匹配INSERT语句中指定的列数。

对我来说,这个错误说我在INSERT条款中有43条陈述,而VALUES条款中我没有43条陈述。然而,每次我查看都有一场比赛。

除了手动浏览数据之外,还有一种更简单的方法来解决这类数据的问题吗?

4 个答案:

答案 0 :(得分:2)

我认为您期望每组()都是一列价值观。那些是行。该组中的每个项目都是一列。

您需要一次插入一行。

VALUES(User_Name
,Type
,Site_Name
,Species
,Year_Class
,Tank_Pond
,Letter
,NAME
,Tank_Pond_uID
,...etc)
,
(User_Name
,Type
,Site_Name
,Species
,Year_Class
,Tank_Pond
,Letter
,NAME
,Tank_Pond_uID
,...etc)

这是有道理的,因为数据作为行存储在数据库中。它们不存储为列,因此(基本上)每个操作都是基于行的操作。

答案 1 :(得分:2)

因为它更易读,您可以考虑将要导入的数据放入CSV,然后将 导入SQL,而不是以当前格式写出来。更容易确保所有内容都按照您的要求进行排列。以下是CSV导入的操作方法,假设您使用的是SQL Server Management Studio:https://support.discountasp.net/kb/a1179/how-to-import-a-csv-file-into-a-database-using-sql-server-management-studio.aspx

答案 2 :(得分:2)

“VALUES”之后的括号中有什么是试图插入的内容。因此,目前您正在尝试将6批“Chase Ayers”插入43行。插入的第一行是这个(然后只需要调整剩下的5行插入) - 注意要插入的所有值都在1组括号内:

INSERT INTO aa_Tanks_Ponds ([User_Name],[Type],Site_Name,Species,Year_Class,Tank_Pond,Letter,[Name],Tank_Pond_uID,Start_Inventory_#,Start_Inventory_lbs,Start_Inventory_avg,Stocking_#,Stocking_lbs,Stocking_from,Stocking_Total_#,Stocking_Total_lbs,Sales_#,Sales_lbs,Sales_Total_#,Sales_Total_lbs,Mortality_#,Mortality_lbs,Transfers_#,Transfers_lbs,Transfers_To,Transfer_Total_#,Transfer_Total_lbs,Plus_Min_#,Plus_Min_lbs,Plus_Min_Total_#,Plus_Min_Total_lbs,Feed_lbs_Tanks_Ponds,Feed_growth_feed,Feed_growth_FCR,Feed_growth_gain,Final_Inventory_#,Final_Inventory_lbs,Final_Inventory_avg,bg_color,tx_color,sort_order,FeedValue)
VALUES ('Chase Ayers',NULL,'Dry Creek','Sturgeon','14C','Tank','H','H1','DCH1',2985,30144,10.1,0,0,' ',0,0,0,0,0,0,0,0,0,0,' ',0,0,,0,0,0,0,0,0,2,0,2985,30144,10.1,'FFFF00','996600',0,NULL);

答案 3 :(得分:1)

  

在SQL Server Management Studio中,可以使用内置的“错误列表”窗格轻松跟踪错误。可以在“视图”菜单中激活此窗格,也可以使用快捷键 Ctrl + \和Ctrl + E

了解更多here