Sql错误列名或提供的值数与表定义

时间:2016-08-29 09:12:27

标签: sql sql-server

我从下面的查询中收到此错误

尽管列数相等,但为什么会发生错误。

Msg 213, Level 16, State 1, Line 46
Column name or number of supplied values does not match table definition.

第46行是INSERT INTO #tmp_statement

查询

DECLARE @PurchaseInvoiceID int,
        @PurchaseInvoiceNo varchar(max),
        @PurchaseInvoiceDate date,
        @Debit numeric(9, 2),
        @Balance numeric(9, 2)
SET @Balance = 0;
CREATE TABLE #tmp_statement (
  PurchaseInvoiceID int,
  PurchaseInvoiceNo  varchar(max),
  PurchaseInvoiceDate date
)
DECLARE rt_cursor CURSOR FOR
SELECT
  dbo.PurchaseInvoices.PurchaseInvoiceID,
  dbo.PurchaseInvoices.PurchaseInvoiceNo,
  dbo.PurchaseInvoices.PurchaseInvoiceDate,
  CONVERT(decimal(9, 2), SUM(RequisitionDetails.ReceivedQuantity * RequisitionDetails.UnitPrice)) AS Debit
FROM dbo.PurchaseOrders
INNER JOIN dbo.Requisitions
  ON dbo.PurchaseOrders.RequisitionID = dbo.Requisitions.RequisitionID
INNER JOIN dbo.Restaurants
  ON dbo.Requisitions.RestaurantID = dbo.Restaurants.RestaurantID
INNER JOIN dbo.Suppliers
  ON dbo.PurchaseOrders.SupplierID = dbo.Suppliers.SupplierID
INNER JOIN dbo.Categories
  ON dbo.Requisitions.CategoryID = dbo.Categories.CategoryID
INNER JOIN dbo.PurchaseInvoices
  ON dbo.PurchaseOrders.PurchaseOrderID = dbo.PurchaseInvoices.PurchaseInvoiceID
INNER JOIN dbo.RequisitionDetails
  ON dbo.RequisitionDetails.RequisitionID = dbo.Requisitions.RequisitionID
INNER JOIN dbo.AccountingDocuments
  ON dbo.PurchaseInvoices.DocumentID = dbo.AccountingDocuments.DocumentID
INNER JOIN dbo.Statement
  ON dbo.PurchaseInvoices.PurchaseInvoiceID = dbo.Statement.PurchaseInvoiceID
WHERE Suppliers.SupplierID = 1
AND dbo.PurchaseInvoices.PurchaseInvoiceDate BETWEEN '2016-08-1' AND '2016-08-31'
GROUP BY dbo.PurchaseOrders.PurchaseOrderID,
         dbo.PurchaseInvoices.PurchaseInvoiceDate,
         dbo.PurchaseInvoices.PurchaseInvoiceNo,
         dbo.PurchaseInvoices.PurchaseInvoiceID
OPEN rt_cursor
FETCH NEXT FROM rt_cursor INTO @PurchaseInvoiceID, @PurchaseInvoiceNo, @PurchaseInvoiceDate, @Debit
WHILE @@FETCH_STATUS = 0
BEGIN
  SET @Balance = @Balance + @Debit 
  INSERT INTO #tmp_statement
    VALUES (@PurchaseInvoiceID, @PurchaseInvoiceNo, @PurchaseInvoiceDate, @Debit,@Balance)
  FETCH NEXT FROM rt_cursor INTO @PurchaseInvoiceID, @PurchaseInvoiceNo, @PurchaseInvoiceDate, @Debit
END
CLOSE rt_cursor
DEALLOCATE rt_cursor
SELECT
  *
FROM #tmp_statement

4 个答案:

答案 0 :(得分:3)

  

我从下面的查询中收到此错误。虽然列数相等,但是发生了错误。

他们不平等..

你的桌子..

CREATE TABLE #tmp_statement (
  PurchaseInvoiceID int,
  PurchaseInvoiceNo  varchar(max),
  PurchaseInvoiceDate date
)

您的插入..

 INSERT INTO #tmp_statement
    VALUES (@PurchaseInvoiceID, @PurchaseInvoiceNo, @PurchaseInvoiceDate, @Debit,@Balance)

答案 1 :(得分:1)

您已使用以下列声明了该表:

  • PurchaseInvoiceID int
  • PurchaseInvoiceNo varchar(max),
  • PurchaseInvoiceDate date

然后,您尝试在INSERT中指定太多列。

VALUES (@PurchaseInvoiceID, @PurchaseInvoiceNo, @PurchaseInvoiceDate, @Debit,@Balance)

列定义需要匹配,因此您可以删除@Debit@Balance,如下所示。

INSERT INTO #tmp_statement
VALUES (@PurchaseInvoiceID, @PurchaseInvoiceNo, @PurchaseInvoiceDate)

或者将借记和余额列定义添加到#tmp_statement表。

答案 2 :(得分:1)

如果要在其中插入值,则需要为@Debit和@Balance创建列。

答案 3 :(得分:1)

你的临时表#tmp_statement用3列声明。

CREATE TABLE #tmp_statement (
  PurchaseInvoiceID int,
  PurchaseInvoiceNo  varchar(max),
  PurchaseInvoiceDate date
)

你插入语句有5列。

INSERT INTO #tmp_statement
VALUES (@PurchaseInvoiceID, @PurchaseInvoiceNo, @PurchaseInvoiceDate, @Debit,@Balance)

所以错误是正确的 要修复它,您需要从insert语句中删除@Debit和@Balance,或者将这些列添加到create table语句中。