I get the following error message with SQL Server 2005
Msg 120, Level 15, State 1, Procedure EPIN, Line 37
The select list for the INSERT statement contains fewer items than the insert list. The number of SELECT values must match the
number of INSERT columns.
I have copy and pasted the select list and insert list into excel and verified there are the same number of items in each list. Both tables an additional primary key field with is not listed in either the insert statement or select list. I am not sure if that is relevant, but suspicious it may be.
Here is the source for my stored procedure:
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
-- =============================================
-- Author: Kristian Manuel
-- Create date: 10/3/2012
-- Description:
-- =============================================
ALTER PROCEDURE [dbo].[EPIN]
-- Add the parameters for the stored procedure here
@PLUCODE varchar(30), --Table tblPolicy
@ECPAYFEES numeric(18,2),
@COSTPAYABLE numeric(18,2),
@RETAILPRICE numeric(18,2),
@DENOMINATION varchar(50),
@CARDNAME varchar(50),
@DistributorID varchar(8),
@AccntID decimal(18,0)
--PO1
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
INSERT INTO [ECPAY-PC].[GENESIS].[dbo].[tblPolicy]
(
PolicyID ,
AccountTID ,
DistributorID ,
CARDNAME ,
DENOMINATION ,
RETAILPRICE ,
COSTPAYABLE ,
ECPAYFEES ,
PLUCODE
)
SELECT
t.*
FROM
(SELECT
AccountTID = @AccntID,
DistributorID = @DistributorID,
CARDNAME = @CARDNAME,
DENOMINATION = @DENOMINATION,
RETAILPRICE = @RETAILPRICE,
COSTPAYABLE = @COSTPAYABLE,
ECPAYFEES = @ECPAYFEES,
PLUCODE = @PLUCODE) t,
[ECPAY-PC].[ECPNWEB].[dbo].[account] a
WHERE
a.AccntID = t.AccountTID --for account
END
The error message is pretty clear – your
INSERTstatement expects 9 columnsto be filled – but your
SELECTonly provides 8 valuesIt seems you’re not providing any value for the
PolicyIDcolumn (first column in yoruINSERT) from yourSELECTstatement…..Update: if your
PolicyIDcolumn in your target table is an “auto-increment” (orIDENTITY) column – then you must not include in in yourINSERTstatement!Just use this:
If you omit the
PolicyIDfrom yourINSERTstatement, then SQL Server will automatically assign anidentityvalue to it and yourINSERT .... SELECTstatement will not work since you’re providing 8 values to anINSERTwhich expects 8 columns…